2. LIBRARIES
It is a file containing compiled code that is to be
incorporated later into a program.
It is of two types:
1. Static library
2. Shared library
3. Static Library
● An archive (or static library) is simply a collection of
object files stored as a single file.
● Static libraries end with the ``.a'' suffix. This collection
is created using the ar (archiver) program.
● When the linker encounters an archive on the
command line, it searches the archive for all
definitions of symbols (functions or variables) that are
referenced from the object files that it has already
processed but not yet defined.
● The object files that define those symbols are extracted
from the archive and included in the final executable.
4. Creating a static library
● Compile: gcc -Wall -g -c prog1.c prog2.c
-Wall: include warnings
-g : include debugging info
● Create library:ar rcs libname.a prog1.o prog2.o
● List files in library: ar -t libctest.a
5.
6. ● Linking with the library:
➢ gcc -o executable-name prog.c libname.a
➢ gcc -o executable-name prog.c -L/path/to/library-
directory -lname
7.
8. Shared Library
● A shared library (also known as a shared object, or as
a dynamically linked library) is similar to a archive in
that it is a grouping of object files.
● The most fundamental difference is that when a shared
library is linked into a program, the final executable
does not actually contain the code that is present in the
shared library. Instead, the executable merely contains
a reference to the shared library.
9. Creating a shared library
● Create library:
gcc -Wall -fPIC -c prog1.c prog2.c
gcc -shared -o libname.so prog1.o prog2.o
● You can create a seperate directory for your
shared libraries. Eg /opt/lib where opt is a
directory that is reserved for all the software
and add-on packages that are not part of the
default installation.
10. ● You can move the created shared library to that directory.
Eg: mv libname.so /opt/lib
● If you are creating the library with version number eg
libname.so.x where x is the version number you have to
link the file as shown below.
ln -sf /opt/lib/libname.so.x /opt/lib/libname.so
● The link to /opt/lib/libname.so allows the naming
convention for the compile flag -lctest to work.
11.
12. ● Compile main program and link with shared object
library:
gcc -Wall -o prog prog.c -L/opt/lib -lname
● The shared library dependencies of the executable
can be listed with the command: ldd name-of-
executable
eg: ldd prog
13. ● The output of ldd may show something like
below:
libname.so => not found
● You can fix this by following one of the
method below:
a) export LD_LIBRARY_PATH=/opt/lib
14.
15. b) During compiling you could add rpath as below
gcc -o prog prog.c -L/opt/lib -lname
-Wl,-rpath=/opt/lib
c) You can add the path of the library in
etc/ld.so.conf.d/libc.conf
● To update the cache use
sudo ldconfig
16.
17. ● Now if you check the dependencies the not found
would have disappeared.
● In LD_LIBRARY_METHOD the environmental
variable vanishes once you close the terminal so
you have to specify the path again and again
● In rpath the path is added to the executable so you
dont have to mention it again for that one.
18. ● If you have added the library path in libc.conf the
libraries will be searched there automatically.
● Note:
➢ Suppose that both libname.a and libname.so are
available.Then the linker must choose one of the
libraries and not the other.
➢ The linker searches each directory (first those
specified with -L options, and then those in the
standard directories).
19. ➢ When the linker finds a directory that contains either
libtest.a or libtest.so, the linker stops searching the
directories.
➢ If only one of the two variants is present in the directory,
the linkerchooses that variant.
➢ Otherwise, the linker chooses the shared library version,
unless you explicitly instruct it otherwise.You can use the
-static option to demand static archives.
➢ For example, the following line will use the libname.a
archive, even if the libname.so shared library is also
available:
gcc -static -o prog prog.c -L/opt/lib –lname
20. ● These shared libraries are dynamically linked.
● Even though the dynamic linker does a lot of the work for
shared libraries, the traditional linker still has a role to play
in creating the executable.
● The traditional linker needs to leave a pointer in the
executable so that the dynamic linker knows what library
will satisfy the dependencies at runtime.
● The dynamic section of the executable requires a
NEEDED entry for each shared library that the executable
depends on.
21.
22. ● The dynamic linker is the program that manages
shared dynamic libraries on behalf of an
executable.
● The essential part of the dynamic linker is fixing up
addresses at runtime, which is the only time you
can know for certain where you are loaded in
memory. A relocation can simply be thought of as
a note that a particular address will need to be fixed
at load time.
23. ● Shared libraries can also be loaded at times other than
during the startup of a program. They're particularly
useful for implementing plugins. Eg adobe plugin in
browser..
● To use dynamic loading functions,include the
<dlfcn.h> header file and link with the –ldl option to
pick up the libdl library.
24. Functions in dynamic loaading
● The dlopen function opens a library
void * dlopen(const char *filename, int flag);
eg:
dlopen("/opt/lib/libname.so", RTLD_LAZY);
RTLD_LAZY: If specfied there is no concern
about unresolved symbols until they are
referenced.
25. RTLD_NOW: All unresolved symbols are resolved
when dlopen() is called.
● dlerror()
Errors can be reported by calling dlerror(), which
returns a string describing the error from the last
call to dlopen(), dlsym(), or dlclose(). One point to
note is that after calling dlerror(), future calls to
dlerror() will return NULL until another error has
been encountered.
26. ● dlsym()
The return value from the dlopen function is a void
* that is used as a handle for the shared library.You
can pass this value to the dlsym function to obtain
the address of a function that has been loaded with
the shared library.
● dlclose()
The dlclose function unloads the shared library.
27. ● The executable is created for dynamic loading
as below
gcc -rdynamic -o progdl progdl.c -ldl
● Thus library is dynamically loaded.
31. Benefits of Static Libraries
● Static libraries cannot eliminate duplicated
code in the system. However, there are
other benefits to using static libraries. Some
of these benefits are as follows:
● Static libraries are simple to use.
● The static library code does not need to be
position-independent code.
32. Benefits of Shared Libraries
● Code sharing saves system resources.
● Several programs that depend on a common
shared library can be fixed all at once by
replacing the common shared library.
● The environment can be modified to use a
shared library.
● Programs can be written to load dynamic
libraries without any prior arrangement at link
time.