LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-18-2010, 09:51 AM   #1
posix_memalign
Member
 
Registered: Aug 2008
Posts: 81

Rep: Reputation: 15
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.
 
Old 04-18-2010, 10:07 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by posix_memalign View Post
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' ?
 
Old 04-18-2010, 12:03 PM   #3
posix_memalign
Member
 
Registered: Aug 2008
Posts: 81

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Sergei Steshenko View Post
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
 
Old 04-18-2010, 12:15 PM   #4
posix_memalign
Member
 
Registered: Aug 2008
Posts: 81

Original Poster
Rep: Reputation: 15
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?
 
Old 04-18-2010, 12:29 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by posix_memalign View Post
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.
 
Old 04-18-2010, 12:43 PM   #6
posix_memalign
Member
 
Registered: Aug 2008
Posts: 81

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Sergei Steshenko View Post
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?
 
Old 04-18-2010, 12:48 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by posix_memalign View Post
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.
 
Old 04-18-2010, 12:58 PM   #8
posix_memalign
Member
 
Registered: Aug 2008
Posts: 81

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Sergei Steshenko View Post
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?
 
Old 04-18-2010, 01:03 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by posix_memalign View Post
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/ .
 
Old 04-18-2010, 01:06 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
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.
 
Old 04-18-2010, 01:07 PM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
http://forum.compiz.org/viewtopic.ph...t=5275&p=36055
 
Old 04-18-2010, 01:35 PM   #12
posix_memalign
Member
 
Registered: Aug 2008
Posts: 81

Original Poster
Rep: Reputation: 15
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.
 
Old 04-18-2010, 02:21 PM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by posix_memalign View Post
...
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.
 
Old 04-18-2010, 02:27 PM   #14
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 114Reputation: 114
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
static and dynamic linking zb10948 Programming 1 04-27-2009 03:48 AM
gcc errors on static, but NOT dynamic, linking MALDATA Programming 5 10-02-2008 11:20 PM
LINUX - linking archive (static library) with shared (dynamic) library gurkama Programming 5 03-04-2007 11:11 PM
Linking a static library to a dynamic one delta4s Programming 2 09-28-2006 01:24 AM
Static/dynamic linking ugenn Linux - Software 0 12-15-2002 11:01 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration