Code::Blocks Forums

User forums => General (but related to Code::Blocks) => Topic started by: poda19 on February 08, 2012, 10:41:29 am

Title: how to static link a library
Post by: poda19 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

Title: Re: how to static link a library
Post by: oBFusCATed 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.
Title: Re: how to static link a library
Post by: poda19 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.
Title: Re: how to static link a library
Post by: Alpha 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 (http://en.wikipedia.org/wiki/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 (http://wiki.wxwidgets.org/WxWidgets_Build_Configurations) (SHARED=0).
Title: Re: how to static link a library
Post by: poda19 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
Title: Re: how to static link a library
Post by: Alpha 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:
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 (http://docs.wxwidgets.org/stable/wx_librarieslist.html) (or trunk libraries list (http://docs.wxwidgets.org/trunk/page_libs.html))
Title: Re: how to static link a library
Post by: stahta01 on February 09, 2012, 05:06:33 am
Read info on http://wiki.codeblocks.org/index.php?title=WxWindowsQuickRef (http://wiki.codeblocks.org/index.php?title=WxWindowsQuickRef)

Turn on Full Compiler Logging
http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28errors%29#Q:_How_do_I_troubleshoot_a_compiler_problem.3F (http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28errors%29#Q:_How_do_I_troubleshoot_a_compiler_problem.3F)

Also read this FAQ
http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28general%29#Q:_I_would_like_to_compile_a_project_using_some_non-standard_libraries._How_can_I_indicate_to_CodeBlocks_that_these_libraries_and_include_files_exist.3F (http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28general%29#Q:_I_would_like_to_compile_a_project_using_some_non-standard_libraries._How_can_I_indicate_to_CodeBlocks_that_these_libraries_and_include_files_exist.3F)

FYI: This is NOT a real Code::Blocks question; but, you are likely to find one soon.

Tim S.
Title: Re: how to static link a library
Post by: poda19 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.
Title: Re: how to static link a library
Post by: Alpha on February 10, 2012, 02:27:47 am
sorry i didn't have access to internet for some hours.
No problem.