
In programming, we use libraries to store common functions that will be used one or more time to gain time.
There are two types of libraries:
- static libraries
- dynamic libraries
Static Libraries : result of the linker making copy of all used library functions to the executable file. Program will be fast and portable but heavier. static libraries have .a extension.
Dynamic libraries : just placing name of the library in the binary file. The actual linking happens when the program is run. Dynamic libraries have .so extension.
create static library :
create objects file of the function .o
gcc -c -Wall -Werror -fpic foo.c
create the static library .a
ar rcs lib_foo.a lib_foo.o
Link Statically:
gcc main.c -L. -llib_foo -o toto
create and use dynamic library :
create objects file of the function
gcc -c -Wall -Werror -fpic foo.c
create shared object
gcc -shared -o libfoo.so foo.o
Linking with a shared library
gcc -Wall -o test main.c -lfoo
define the path of the objects to the current directory
LD_LIBRARY_PATH=/home/username/foo:$LD_LIBRARY_PATH