Next: Dynamic Linking and Loading
Up: Shared Library
Previous: Shared Library
At the link time, the linker search throught the libraries (specified by -L option)
to find the modules that resolve underfined external symbol.
However if the library is a shared one, the library will not be copied into the
output file.
Instead the linker just makes some notes of what library the module came from, and put the notes in the output file.
When the program is loaded, loader will try to find the libraries and maps them into program's address space before the program start.
So for a shared library, the symbol resolving is sperated from the symbol binding;
A program that uses a shared library depends on having the shared library available when the program is running
Another problem caused by the seperated binding time is the shared lib which is used for the symbol resolution is
not the same one which is used for binding at the begining of running, and the difference may lead havoc.
Tthe library has been changed a little bit (e.g. the bug fix),
but doesn't affect the structure of the whole library and the correct binding,
we call this version be the minor version.
To create the shared library, in linux the command is
g++ -o libfoo.so bar.o foo.o -shared -Wl,-soname=libfoo.so
option ``-Wl'' meansing passing the argument to the linker;
``soname'' is used to assigne an internal name for the shared library
so that the loader can find the proper library for binding;
It should be stressed that soname has to be ``libfoo.so'',
neither ``foo'' nor ``libfoo'' works when searching the lib at running time;
The size of the lib file is bigger(why?) compared with the archive file,
and it is ELF object format;
-rw------- 1 welu students 1676 Oct 8 21:45 libfoo.a
-rwx------ 1 welu students 6722 Oct 9 00:05 libfoo.so
Now we link the shared lib with main.
g++ -o main main.o -lfoo -L./ -Wl,-rpath=`pwd`
The option ``-rpath'' tells the linker to put the path to the shared lib into the output file,
so that when the binding time, we can search the specified path for the specified lib;
more detail on library in Linux
cross platform options
How to write shared library
(LD_LIBRARY_PATH,the library may be specified in the executable by rpath, or )
Next: Dynamic Linking and Loading
Up: Shared Library
Previous: Shared Library
Wei Lu
2007-11-06