Author Topic: how to static link a library  (Read 13370 times)

Offline poda19

  • Multiple posting newcomer
  • *
  • Posts: 10
how to static link a library
« on: February 08, 2012, 10:41:29 am »
i want to statically link a library (wxmsw28u.a) to my program so it will not request for wxmsw28u_gcc.dll when i run it on other machines.
i have googled it since yesterday but i found no results. there where two other topics here asking my question too but there where no proper answer to them.
i have added wxmsw28u.a in Linkersettings->Link libraries and have added "-static" to other linker options.
but nothing happens
am i doing something wrong?
i have never linked a library before, is there any other things i should do to statically link a library?
thanks

"Common sense is the most fairly distributed thing in the world, for each one thinks he is so well-endowed with it."<br />Rene Descartes

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: how to static link a library
« Reply #1 on: February 08, 2012, 12:29:59 pm »
If you've build the wxwidgets static library correctly you should not have trouble doing it c::b.
Have you read the documentation of wxwidgets about compiling and linking your project to the various versions of wxwidgets?
Keep in mind that wxWidgets is pretty complex library and there are many things you should have in sync (defines on the command line, defines in setup.h, linking options) in order to build, link and run it correctly.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline poda19

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: how to static link a library
« Reply #2 on: February 08, 2012, 09:56:50 pm »
OH, please for the sake of GOD
it is two days i am searching whole the internet and asking in forums and no one is going to answer my question. NOWHERE ON THE NET (if you don't beleive me try it. i dare you) i could find a peice of instruction on what the heck is static linking. all they say is "if you want to eliminate the need of dlls use static linking, but it makes the file size bigger." i don't know if static linking is an innate behavior like swallowing that everyone knows how to do or i am such an stupid person.
i'm not a computer student or a professional programmer, and have no access to a teacher or an informed person. just interested in programming and trying to write a junk program that is not useful for anyone on the earth.
so if there is anyone who could mind showing me some source of a little bit of explanation on linking or a tiny example about how do people statically link their libraries, i would be so very very thankful.
i know you have strict rules here on post contents and i'm sorry for my tone but i'm feeling really desperate.
"Common sense is the most fairly distributed thing in the world, for each one thinks he is so well-endowed with it."<br />Rene Descartes

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: how to static link a library
« Reply #3 on: February 08, 2012, 10:56:50 pm »
NOWHERE ON THE NET (if you don't beleive me try it. i dare you) i could find a peice of instruction on what the heck is static linking.
Static library
Quote from: Wikipedia
[...] a static library or statically-linked library is a set of routines, external functions and variables which are [...] copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.

i have added wxmsw28u.a in Linkersettings->Link libraries and have added "-static" to other linker options.
What is required here, is for you to look under Search directories->Linker, and change the path that should look something like $(#wx.lib)\gcc_dll to $(#wx.lib)\gcc_lib (this is because wxWidgets chooses to have both static and shared libraries use the same name, but located in different directories).  Note that this will only work if your computer also contains a static build of wxWidgets (SHARED=0).

Offline poda19

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: how to static link a library
« Reply #4 on: February 08, 2012, 11:41:34 pm »
i have already read wikipedia(and many other sources) . it has just explained how cool is static linking and has a tiny explanation on creating static libraries. not how to link a library to a program.
Quote
NOWHERE ON THE NET (if you don't beleive me try it. i dare you) i could find a peice of instruction
i need an INSTRUCTION, but all that is there in the web(including wikipedia) is INTRODUCTION ;)
i tried what you suggested and there is a bunch of "undefined reference to _imp__wx...".
i don't think my libraries have shared=0, i didn't build them. i installed wxPack.
anyway it's 1:50am and i'm convinced that i can't make it. i give up static linking right here right now.

thanks for your reply Alpha
"Common sense is the most fairly distributed thing in the world, for each one thinks he is so well-endowed with it."<br />Rene Descartes

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: how to static link a library
« Reply #5 on: February 09, 2012, 02:51:08 am »
Sorry, I just realized my instructions do not exactly fit your situation (they were written to apply to a pre-existing multi-lib configuration).
Under link libraries, you will need a minimum of:
  • wxbase28u
  • wxmsw28u_core
You will also need to add libraries for any additional features you use (for example wxmsw28u_media and wxpng).

The easiest way to set this up is to run the wxWidgets project wizard, uncheck Use wxWidgets DLL and wxWidgets is built as a monolithic library, and select the feature(s) you plan to use from the last page in the wizard.  (If you do not know what to choose, select everything except for DbGrid, Odbc, Gl, and Qa.)

i don't think my libraries have shared=0, i didn't build them. i installed wxPack.
From what I remember, wxPack includes all build configurations (which is why it is so large!!); also, GCC would warn about not being able to find the file (instead of saying "undefined reference ...") if this were the case.

Edit: See also: libraries list (or trunk libraries list)
« Last Edit: February 09, 2012, 02:55:26 am by Alpha »

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7588
    • My Best Post
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline poda19

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: how to static link a library
« Reply #7 on: February 09, 2012, 09:57:59 pm »
sorry i didn't have access to internet for some hours.
i did what you said Alpha and unchecked the use libraries and monolithic options. and when i builded my project it returned lots of errors(that was why i did never unchecked them before). i builded my wxwidgets libraries myself(according to wxwidgets docs) and then my project was built and everything went ok.
thank you very much for your responses specially Alpha. i really didn't think everything wil be fixed just by unchecking those two options.
"Common sense is the most fairly distributed thing in the world, for each one thinks he is so well-endowed with it."<br />Rene Descartes

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: how to static link a library
« Reply #8 on: February 10, 2012, 02:27:47 am »
sorry i didn't have access to internet for some hours.
No problem.