LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-19-2006, 08:54 AM   #1
trek413
LQ Newbie
 
Registered: Aug 2006
Posts: 15

Rep: Reputation: 0
Compiling with static libraries


I would like to build a program (vlc specifically, but the issue
is in general) with the libraries statically linked. My
understanding is that by statically linking the libraries,
the parts of the libraries needed by the program will be
compiled into the binary, as opposed to being referenced
to shared libraries via dynamic linking. Hence, the resulting
binary is larger as a consequence of more code being built in, but
it is then able to run without the libraries it depends on
being accessible.

If this is correct, how would you go about statically linking
libraries? Is it just an option that you list as a compiler
flag?

Thanks.
 
Old 10-19-2006, 09:30 AM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Try adding CFLAGS+=" -static " and LDFLAGS+=" -static "

That said, you may ask how you go about testing to make sure a file is not linked against any dynamic libraries? You can use readelf or objdump. For example, readelf -d '/PATH/TO/ELF/EXECUTABLE' will spit out the dynamic section which should be empty if it is completely static.
 
Old 10-19-2006, 09:52 AM   #3
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
"ldd" is another tool to find out dynamically linked libraries.

And, if you intend to distribute your application, be sure to read the licenses of all the included libraries.
 
Old 10-19-2006, 03:09 PM   #4
trek413
LQ Newbie
 
Registered: Aug 2006
Posts: 15

Original Poster
Rep: Reputation: 0
so, it worked to some degree. I added in the -static flag
to CFLAGS and LDFLAGS and then compiled. ldd yielded:

Code:
ldd ./vlc
        linux-gate.so.1 =>  (0x00c24000)
        libhal.so.1 => /usr/lib/libhal.so.1 (0x0559a000)
        libdbus-1.so.2 => /lib/libdbus-1.so.2 (0x008ce000)
        librt.so.1 => /lib/librt.so.1 (0x00943000)
        libpthread.so.0 => /lib/libpthread.so.0 (0x00101000)
        libdl.so.2 => /lib/libdl.so.2 (0x00adc000)
        libz.so.1 => /usr/lib/libz.so.1 (0x00ae2000)
        libmp3lame.so.0 => /usr/local/lib/libmp3lame.so.0 (0x00efc000)
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x05c8e000)
        libm.so.6 => /lib/libm.so.6 (0x00ab5000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00821000)
        libc.so.6 => /lib/libc.so.6 (0x00980000)
        libcap.so.1 => /lib/libcap.so.1 (0x0089d000)
        libnsl.so.1 => /lib/libnsl.so.1 (0x005fa000)
        /lib/ld-linux.so.2 (0x00963000)
This is certainly better than it was, as running ldd on
the binary compiled without the -static flags yielded:

[HTML]ldd `which vlc`
linux-gate.so.1 => (0x00db0000)
libhal.so.1 => /usr/lib/libhal.so.1 (0x0559a000)
libdbus-1.so.2 => /lib/libdbus-1.so.2 (0x008ce000)
librt.so.1 => /lib/librt.so.1 (0x00943000)
libpthread.so.0 => /lib/libpthread.so.0 (0x00101000)
libdl.so.2 => /lib/libdl.so.2 (0x00adc000)
libdvbpsi.so.4 => /usr/lib/libdvbpsi.so.4 (0x00af7000)
libmatroska.so.0 => /usr/lib/libmatroska.so.0 (0x00b05000)
libebml.so.0 => /usr/lib/libebml.so.0 (0x00bb7000)
libz.so.1 => /usr/lib/libz.so.1 (0x00ae2000)
libfaac.so.0 => /usr/lib/libfaac.so.0 (0x0094d000)
libmp3lame.so.0 => /usr/lib/libmp3lame.so.0 (0x005a8000)
libtheora.so.0 => /usr/lib/libtheora.so.0 (0x052ef000)
libogg.so.0 => /usr/lib/libogg.so.0 (0x00115000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x05c8e000)
libm.so.6 => /lib/libm.so.6 (0x00ab5000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00821000)
libc.so.6 => /lib/libc.so.6 (0x00980000)
libcap.so.1 => /lib/libcap.so.1 (0x0089d000)
libnsl.so.1 => /lib/libnsl.so.1 (0x0011b000)
/lib/ld-linux.so.2 (0x00963000)
libmp4v2.so.0 => /usr/lib/libmp4v2.so.0 (0x055a9000)
[/HTML]

Would I be right to assume that any libraries which it depends
on only exist as a shared object? It is quite possible I do not
have the static versions of those libraries installed.
 
Old 10-19-2006, 07:14 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by trek413
Would I be right to assume that any libraries which it depends
on only exist as a shared object? It is quite possible I do not
have the static versions of those libraries installed.
That would make sense to me. You should probably check first though. What I would do first is do a `locate linux-gate.a' (along with all the others) to see if such a library exists. If it does exists, maybe it's outside the ld search-path? (One of many ways) to find out is `ld --verbose | grep SEARCH_DIR' and comparing the locations to what you found above. If it is in the path, there must be something screwy going on (maybe in a linker script?).

Anyway, you always have a last resort: link explicitly to each library (e.g., gcc -o vlc foo.o bar.o baz.o /path/to/linux-gate.a /path/to/libhal.a etc.). You might have to tinker around with the Makefile to figure out how to accomplish this specifically for vlc.
 
  


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 variable sharing in shared libraries ristiisa Programming 13 10-20-2006 09:08 AM
Hiding Symbols in Static Libraries JCipriani Programming 2 03-03-2006 05:15 PM
Undefined references with static libraries Nightfox Programming 2 10-28-2005 06:43 PM
compiling with shared and static libraries jinksys Programming 1 11-22-2003 04:54 AM
gaim trying to link static and shared libraries rose_bud4201 Linux - Software 0 09-28-2003 12:27 AM


All times are GMT -5. The time now is 12:29 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