GCC
Shared Libraries
Naming convention
Real Name
all libraries do not follow the convention
- lib
.so.x.y.z (libjpeg.so.1.2.3) - lb
-x.yy.so (libc-2.31.so)
name -> name of the Library x -> major version number y -> minor version number z -> patch version number
Shared Object Name (soname)
- Label for a major version of a shared object
- soname: libjpeg.so.1 => libjpeg.so.1.2.3
- helps to resolve shared library dependencies
Linker Name
- gcc will look for this during
- libname.so
To create a shared library
source code -> object file -> shared library
- gcc -fpic -c source.c
- creates an object file
source.owith position-independent code (PIC)
- creates an object file
- gcc -fpic source1.o source2.o -shared -Wl,-soname,libjpeg.so.1 -o libjpeg.so.1.2.3
- creates a shared library from the object file
- utilize the created shared library during compiling and linking
- gcc main.c -L. -ljpeg -o main
-L.tells the linker to look in the current directory for libraries-ljpegtells the linker to link againstlibjpeg.so(the linker will resolve this tolibjpeg.so.1.2.3based on the soname)
- gcc main.c -L. -ljpeg -o main
LB_LiBRARY_PATH environment variable
- specifies a list of directories where the linker should look for shared libraries at runtime
- if the shared library is not found in the standard locations, the linker will search the directories
Shared object libraries (.so files) on Linux/Unix systems are primarily located in standard paths like /lib, /usr/lib, and /usr/local/lib. The dynamic linker (ld.so) searches these directories, along with paths defined in /etc/ld.so.conf and LD_LIBRARY_PATH`, to resolve library dependencies at runtime
Static Libraries
References:
- https://www.youtube.com/watch?v=UdMRcJwvWIY