views:

395

answers:

7

Hi.

I seen several discussions here on the subject, but wanted to ask about my particular situation:

If I have some 3rd part libraries which my application is using, and I'd like to link them together in order to save myself the hassle in LD_LIBRARY, etc., is there any downside to it on Linux, other then larger file size?

Also, is it possible to statically link only some libraries, and other (standard Linux libraries) to link dynamically?

Thanks.

+1  A: 

The statically linked binary will be larger than if you had uses a shared library, but I find that disadvantage outweight the library path hassles, provided I control the distribution of all the libraries involved. If you are dependant on a particular distros shared libraries, then you have no choice but to use dynamic linking.

anon
+1  A: 

To answer your second question, yes, it is possible to have dynamic and static libraries linked to the same application. Just be careful to avoid interlibrary dependencies so you don't have a problem with library order. You should be able to list the libraries in any arbitrary order. Where I work, we prefer to list them alphabetically.

Edit: To link a static library, use the flag -lfoo. To add a directory to the library search path, use -L/path/to/libfoo.

Edit: You don't have to link a dynamic library. Your program can use a function provided by your compiler to open a dynamic library at run time, or you can link it at compile time and the compiler will resolve the symbols but not include them in the binary. See pjc50's comment below.

Scottie T
Meaning you just specify two flags, one for static and one for dynamic, and in each one, list them in ABC format?What are the flags that you using?
SyRenity
This is a bit misleading - you definitely do link against dynamic libraries and you can have them loaded implicitly without having to use dlopen().-lfoo will link dynamically against libfoo.so if it can find it, else statically against libfoo.a. When it links against libfoo.so it does not copy the code into the binary, it puts a reference in the file header which tells the OS runtime linker to load libfoo.so when the program is run.
pjc50
Thanks, @pjc50. I didn't know that.
Scottie T
+2  A: 

There are some bits of libc - those that use nsswitch - that need to load libraries dynamically. This can cause problems if you want to produce a completely static binary.

Statically linking your 3rd party libraries into your application should be completely fine.

pjc50
Thanks - are the flags to link are -L for dynamic, and -l for static?
SyRenity
To statically link just add the complete path to the lib*.a-file. -L is used to include certain paths when searching for libraries (during linking) and -l is for dynamic linking.
David Holm
+1  A: 

The main disadvantage I see is your application loses any automatic bugfixes that might be applied to a shared library. On the flip-side you don't get new bugs.

SpliFF
A: 

Statically linking will make your binary bulky, but you wont need to have a shared version of that library on the target runtime environment. This is especially the case while developing embedded apps.

Here Be Wolves
+1  A: 

Static linking does not just affect the file size of the library, it also affects the memory footprint and start up time of the application. Dynamically linked libraries are loaded once no matter how many programs use them. Statically linked libraries must be loaded once per program that uses them (because they are now part of that program).

Chas. Owens
+4  A: 

It is indeed possible to dynamically link against some libraries and statically link against others.

It sounds like what you really want to do is dynamically link against the system libraries, and statically link against the nonstandard ones that a user may not have installed (or that different users may have different installations of).

That's perfectly reasonable.

It's not generally a good idea to statically link against system libraries, especially libc.

It can often make sense to statically link against libraries that do not come with the OS and that will not be distributed with your application.

chuck
Hi.Yes, this what I'm looking to do.What are the flags that I need to pass to the compiler, in order to make it work?Regards.
SyRenity
If you have your libraries in .a format, (eg libfoo.a) then all you need to do is add them to the command line. Remove any -lfoo you may have.So the dynamic version:ld -o result my.o -lfoowould become:ld -o result my.o /path/to/libfoo.a
pjc50