2.3.2. Creating dynamic libraries with the GCC
To build and install a dynamic library from the source code, you can use the GNU Compiler Collection (GCC). Dynamically linked libraries, also known as shared objects, help you conserve resources by reusing code and increase security by making library updates easier.
Prerequisites
- You must understand the soname mechanism.
- GCC must be installed on the system.
- You must have source code for a library.
Procedure
- Change to the directory with library sources.
Compile each source file to an object file with the Position independent code option
-fPIC:$ gcc ... -c -fPIC some_file.c ...The object files have the same file names as the original source code files, but their extension is
.o.Link the shared library from the object files:
$ gcc -shared -o libfoo.so.x.y -Wl,-soname,libfoo.so.x some_file.o ...The used major version number is X and minor version number Y.
Copy the
libfoo.so.x.yfile to an appropriate location, where the system’s dynamic linker can find it. On Red Hat Enterprise Linux, the directory for libraries is/usr/lib64:# cp libfoo.so.x.y /usr/lib64Note that you need root permissions to manipulate files in this directory.
Create the symlink for the soname:
# ln -s libfoo.so.x.y libfoo.so.xCreate the symlink for the linker name:
# ln -s libfoo.so.x libfoo.so
Additional resources