Author Topic: [SOLVED] dynamic versus static linking  (Read 10871 times)

Andy

  • Guest
[SOLVED] dynamic versus static linking
« on: February 13, 2006, 09:09:51 pm »
i have juste recently started using code::blocks and mostly i am satisfied with it. i could not figure out one thing, however. how to i make the linker link some libraries staticly and others dynamicly. i know that i can do that on the command-line by adding the -static linker option before the libraries i want to link as static. in c::b, when i add this option to project build options -> linker -> other linker options, the linker uses it for all the libraries, which is not what i want.

i know that there are the advanced compiler settings but i could not figure out how to use these to achieve this goal either. can anyone help me with this one? thanks :-)
« Last Edit: February 18, 2006, 10:05:11 am by Andy »

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: dynamic versus static linking
« Reply #1 on: February 13, 2006, 09:34:30 pm »
Do you know the difference between dynamically-linked and statically-linked libraries?

Here's how it works if on Windows.
A statically linked library is a single file, usually with a .a or .lib extension, containing all the library code for a program to make use of. When passed to the linker, it is compiled directly into the final executable.
A dynamically linked library is two files: one with a .a or .lib extension, and one with a .dll extension. The library (.a or .lib) file contains only the data necessary to show the linker that the DLL contains the library code. The final executable does not then contain the library code, but rather looks for and loads the DLL when you run it.

On Linux it's mostly the same, except the compiler uses options like "-shared" and "-fPIC" when creating the dynamic library, and the DLL files are instead .so (Shared Object) files.

Either way, you shouldn't need to give the compiler or linker any extra options; it should be able to determine for itself from the library file whether the library is dynamically or statically linked. The -static command line option actually prevents the linker from correctly linking with dynamic libraries.
« Last Edit: February 13, 2006, 09:40:20 pm by TDragon »
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

Andy

  • Guest
Re: dynamic versus static linking
« Reply #2 on: February 13, 2006, 10:07:10 pm »
well, i think i understand how linking and stuff works. my question was aimed at libraries that are available both as static and dynamic. i would like to make a choice in this case. an example is the openglut library. i have these files in /usr/local/lib:

-rw-r--r--  1 root root 557170 Feb  9 16:19 libopenglut.a
-rwxr-xr-x  1 root root    977 Feb  9 16:19 libopenglut.la
lrwxrwxrwx  1 root root     20 Feb  9 16:19 libopenglut.so -> libopenglut.so.1.0.0
lrwxrwxrwx  1 root root     20 Feb  9 16:19 libopenglut.so.1 -> libopenglut.so.1.0.0
-rwxr-xr-x  1 root root 470600 Feb  9 16:19 libopenglut.so.1.0.0

because of the sizes i believe that libopenglut.a is a library for static linking whereas libopenglut.so.1.0.0 is a dynamic library. am i wrong?

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: dynamic versus static linking
« Reply #3 on: February 13, 2006, 10:10:55 pm »
If you link with libopenglut.a, the library code will be placed in the final binary. If you link with libopenglut.la, the binary will load libopenglut.so.1.0.0 at runtime.
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

Andy

  • Guest
Re: dynamic versus static linking
« Reply #4 on: February 13, 2006, 11:49:25 pm »
yes, that is what i thought. so back to my original question: how to i distinguish this in c::b? i can only set openglut as one of the libraries that are to be linked, but i am not able to control the static vs. dynamic difference. so far it is linked with the dynamic library (my app has ~35kB).

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: dynamic versus static linking
« Reply #5 on: February 13, 2006, 11:50:48 pm »
Supply the full filename "libopenglut.a" (or "libopenglut.la").
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

Andy

  • Guest
Re: dynamic versus static linking
« Reply #6 on: February 14, 2006, 12:06:50 am »
i am sorry if i am missing something but that does not seem to work. if i change the name of the library in the build options to libopenglut.a, the command-line produced by c::b does not change (it is the same as with openglut and so the link is dynamic):

g++   -L/usr/local/lib  -L/usr/lib -o SiSi .objs/main.o        -lhistory -lreadline -lGL -lGLU -lopenglut

if i put in openglut.a, i get a linker error:

cannot find -lopenglut.a

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: dynamic versus static linking
« Reply #7 on: February 14, 2006, 12:16:28 am »
Oops...make it a fully-qualified path. (/usr/local/lib/libopenglut.a)

And submit a feature request. Rather, a feature-removal request (don't automatically translate full library names to -l versions).
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

Andy

  • Guest
Re: dynamic versus static linking
« Reply #8 on: February 14, 2006, 12:22:39 am »
great, this works exactly as i wanted. just one last thing - how do i submit a feature request properly? i am kind of new here  :)

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: dynamic versus static linking
« Reply #9 on: February 14, 2006, 12:28:27 am »
If you have or are willing to sign up for an account at BerliOS, you can follow the "Ftr Requests" link from the lower left of the Code::Blocks home page. If not, make a noise on an appropriate board here in the C::B forums or hit mandrav with an email (these are less reliable than an official feature request).

However you do it, be descriptive about what led you to this point, how it's causing you a problem, and what your recommendation for a fix would be.

Cheers,
JohnE / TDragon
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)