From LQWiki
Static linking is when you link in parts of a static library when generating an object file or executable output. It differs from dynamic linking in that it includes the entire library being linked in the executable rather than just allowing the library to be linked automatically whenever the executable is run. The main advantage of static linking over dynamic linking is that since the library is included directly within the executable itself, the computer running the executable doesn't need to have the library installed which can help to reduce dependency hell. However, this ease of use comes at a cost. Firstly, the executable takes up more (often a lot more) space both on the hard drive and in RAM. The program also uses more RAM than a normal one would as the kernel won't allow other processes to share code already loaded into memory. Under a dynamic linking scheme, if two programs are run which both use the same library, the kernel can, to some extent, allow both processes to access the same copy of the library (so rather than loading a copy of the library for each process, it simply loads one and lets both of them access it). This isn't possible with a statically linked binary (executable) however, since the library is part of the program being run.
gcc
With gcc, libraries are linked with "-l". (Note that the library could be either dynamic, or static.) A static library specified with "-lfoo" will be looked up as "libfoo.a".
The library is searched for in standard directories, plus whatever you specify with "-L". The standard directories are:
- /usr/local/lib
- /usr/lib
The order that -l's appear in matters; Symbols are only pulled from -l's that have been requested by an object file before the -l appeared.
example: If foo.o has a symbol that is defined in libbar.a, then:
g++ foo.o -lbar # this works g++ -lbar foo.o # this doesn't work
In the one that doesn't work, nothing is pulled from bar, because no symbols have been requested that are found in it, yet.
See also
This article is a stub and needs to be finished. Plunge forward and help it grow!
External Links
- An Introduction to GCC - Setting search paths (www.network-theory.co.uk)

This page is available under a