LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Static linking vs. dynamic linking? (http://www.linuxquestions.org/questions/programming-9/static-linking-vs-dynamic-linking-802690/)

posix_memalign 04-18-2010 09:51 AM

Static linking vs. dynamic linking?
 
I have written an OpenGL based program which uses i.a. libjpeg that I want to link statically so I can send it to someone who doesn't have all the libraries needed installed to allow for a dynamically linked executable to run.

As such I tried with giving the [b]-static[b] parameter to gcc.

Code:

x86_1$ make
gcc -O3 -c jpegloader.c
gcc --std=c99 -O3 -c --std=c99 -I/usr/include -L/usr/X11R6/lib main.c
gcc -static -o renderer main.o jpegloader.o --std=c99 -O3 -Wall -I/usr/include -L/usr/X11R6/lib -lglut -lGL -lGLU -lm -ljpeg
/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../i486-slackware-linux/bin/ld: cannot find -lglut
collect2: ld returned 1 exit status
make: *** [renderer] Error 1

Why would gcc claim that I'm missing GLUT? This works perfectly fine when I do dynamic linking -- i.e. omitting the -static argument.

Sergei Steshenko 04-18-2010 10:07 AM

Quote:

Originally Posted by posix_memalign (Post 3939384)
I have written an OpenGL based program which uses i.a. libjpeg that I want to link statically so I can send it to someone who doesn't have all the libraries needed installed to allow for a dynamically linked executable to run.

As such I tried with giving the [b]-static[b] parameter to gcc.

Code:

x86_1$ make
gcc -O3 -c jpegloader.c
gcc --std=c99 -O3 -c --std=c99 -I/usr/include -L/usr/X11R6/lib main.c
gcc -static -o renderer main.o jpegloader.o --std=c99 -O3 -Wall -I/usr/include -L/usr/X11R6/lib -lglut -lGL -lGLU -lm -ljpeg
/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../i486-slackware-linux/bin/ld: cannot find -lglut
collect2: ld returned 1 exit status
make: *** [renderer] Error 1

Why would gcc claim that I'm missing GLUT? This works perfectly fine when I do dynamic linking -- i.e. omitting the -static argument.

Do you have static 'libglut' ?

posix_memalign 04-18-2010 12:03 PM

Quote:

Originally Posted by Sergei Steshenko (Post 3939401)
Do you have static 'libglut' ?

I didn't even know I needed a separate static version of libglut. If I do then it comes with my distribution, which is Slackware 12.2, I don't believe I do.

Where can I get it? I don't see anything to it here: http://www.opengl.org/resources/libr..._downloads.php

posix_memalign 04-18-2010 12:15 PM

After reading some I learned I needed the libglut.a file, which I happened to have around for some reason ... after installing it I simply get instead:

Code:

/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../i486-slackware-linux/bin/ld: cannot find -lGL
So now I'm missing the static version of GL? I apparently need a static version of every library I link against?

I also found: http://www.linuxquestions.org/questi...ith-gcc-80200/ which seem to state that this is actually quite tricky to do.

What is the static version of GL called?

Sergei Steshenko 04-18-2010 12:29 PM

Quote:

Originally Posted by posix_memalign (Post 3939499)
After reading some I learned I needed the libglut.a file, which I happened to have around for some reason ... after installing it I simply get instead:

Code:

/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../i486-slackware-linux/bin/ld: cannot find -lGL
So now I'm missing the static version of GL? I apparently need a static version of every library I link against?

I also found: http://www.linuxquestions.org/questi...ith-gcc-80200/ which seem to state that this is actually quite tricky to do.

What is the static version of GL called?

I do not know how static GL library is called; typically it's the same name with a different extension.

You already use -L<path>, so you might need to just another -L<path> for the static library. By the way, you might come to the conclusion that in your case it would be more convenient to call 'ld' directly.

posix_memalign 04-18-2010 12:43 PM

Quote:

Originally Posted by Sergei Steshenko (Post 3939511)
I do not know how static GL library is called; typically it's the same name with a different extension.

You already use -L<path>, so you might need to just another -L<path> for the static library. By the way, you might come to the conclusion that in your case it would be more convenient to call 'ld' directly.

I think it is libGL.a, but I have no idea where to find it, do you have any idea?

Sergei Steshenko 04-18-2010 12:48 PM

Quote:

Originally Posted by posix_memalign (Post 3939525)
I think it is libGL.a, but I have no idea where to find it, do you have any idea?

Typically Linux distros have package managers which allow to find the package containing the needed file. Since I'm using SUSE, I can't help you with (what appears to be) Slackware.

I.e. check your distro package manager features. In the worst case you'll build the library from source.

posix_memalign 04-18-2010 12:58 PM

Quote:

Originally Posted by Sergei Steshenko (Post 3939530)
Typically Linux distros have package managers which allow to find the package containing the needed file. Since I'm using SUSE, I can't help you with (what appears to be) Slackware.

I.e. check your distro package manager features. In the worst case you'll build the library from source.

I'll build it from source, I can't find any package from it, I just need to know the name of it, would you or anyone happen to know the full name of the library I need to download?

Sergei Steshenko 04-18-2010 01:03 PM

Quote:

Originally Posted by posix_memalign (Post 3939538)
I'll build it from source, I can't find any package from it, I just need to know the name of it, would you or anyone happen to know the full name of the library I need to download?

Quick WEB search ( slackware libGL.la ) gave me this link: http://www.linuxquestions.org/questi...k-11-a-677272/ .

Sergei Steshenko 04-18-2010 01:06 PM

Saw this : http://forum.lafox.net/index.php?showtopic=15150 thread in Russian - it appears libGL comes with proprietary NVidia/TI drivers. And at all it might be just libGL.so.

Sergei Steshenko 04-18-2010 01:07 PM

http://forum.compiz.org/viewtopic.ph...t=5275&p=36055

posix_memalign 04-18-2010 01:35 PM

By taking static libraries from another distro (Ubuntu) and simply copy the .a files I needed I have managed to remove all missing libraries that were not found by the linker when attempting to link statically.

However, now I simply get this:

Code:

/usr/X11R6/lib/libglut.a(glut_event.o): In function `interruptibleXNextEvent':
glut_event.c:(.text+0x3ca): undefined reference to `XFlush'
glut_event.c:(.text+0x3d9): undefined reference to `XPending'
glut_event.c:(.text+0x460): undefined reference to `XNextEvent'
/usr/X11R6/lib/libglut.a(glut_event.o): In function `processEventsAndTimeouts':
glut_event.c:(.text+0x4ac): undefined reference to `XRefreshKeyboardMapping'
glut_event.c:(.text+0x4c2): undefined reference to `XPending'
glut_event.c:(.text+0x535): undefined reference to `XLookupString'
glut_event.c:(.text+0x571): undefined reference to `XLookupKeysym'
glut_event.c:(.text+0x6e2): undefined reference to `XEventsQueued'
glut_event.c:(.text+0x701): undefined reference to `XPeekEvent'
glut_event.c:(.text+0x74c): undefined reference to `XNextEvent'
glut_event.c:(.text+0x9af): undefined reference to `XEventsQueued'
glut_event.c:(.text+0x9ca): undefined reference to `XPeekEvent'
glut_event.c:(.text+0xaa4): undefined reference to `XEventsQueued'
glut_event.c:(.text+0xabf): undefined reference to `XPeekEvent'
glut_event.c:(.text+0xae4): undefined reference to `XNextEvent'
glut_event.c:(.text+0xc84): undefined reference to `XResizeWindow'

And so on.

I then tried to copy in the libX11.a library, which fixed some of them, but not nearly all; what else files do I need?

Thanks for the help so far by the way.

Is there a way to link statically against some, but not all libraries? I don't really need to link against X11.

Sergei Steshenko 04-18-2010 02:21 PM

Quote:

Originally Posted by posix_memalign (Post 3939580)
...
what else files do I need?
...

I'd suggest to do the following:
  1. Link first dynamically, even though it's not what ultimately need;
  2. Run 'ldd -v' on the final entity - this will show all the libraries involved;
  3. Find the corresponding static libraries and link everything statically.

I am not sure whether you can link statically against some, but not all libraries, check 'ld' switches for this. Start from 'ldd --help | less' and check all the switches controlling library search places and order. My gut tells me you better create a directory into which you symlink just the needed libraries and make 'ld' look for libraries only in that directory.

Probably you'll need to create a toy program and toy libraries to check static/dynamic libraries mixture.

What you can do for sure is to use 'dlopen' in your program, i.e. in such a manner you deal with dynamic libraries loading.

jiml8 04-18-2010 02:27 PM

You should only statically link the library that you need to have linked statically, not all of them.

It is easy enough to do. Just list the lib you want linked statically as another object file to link.


All times are GMT -5. The time now is 11:10 PM.