Dynamically linked libraries

I’ve read the excellent post about Static, Shared Dynamic and Loadable Linux Libraries at Yo Linux. This post contains notes about this study.

Shared libraries are denoted by the .so extension.

First, it’s necessary to generate object code:

gcc -fPIC -c *.c

The parameter -c tells the compiler to stop before linking stage. The *.c resolve to all .c files in the directory. It’s the same to run the above command for each .c file in the directory. The -fPIC parameter tells the compiler to generate Position Independent Code, which is necessary for shared libraries.

To generate shared libraries from the object files,

gcc -shared -Wl,-soname,libtest.so.1 -o libtest.so.0.1 *o

Let’s comment the parameters:

-shared tells the gcc we are generating a shared library. -Wl means we will pass parameter options to the linker which are, in this case, -soname libtest.so.1. The -o parameter is to tell gcc the output name of the shared object.

According to the man page of the gnu likner (ld), the -soname parameter:

When creating an ELF shared object, set the internal DT_SONAME
field to the specified name. When an executable is linked with a
shared object which has a DT_SONAME field, then when the executable
is run the dynamic linker will attempt to load the shared object
specified by the DT_SONAME field rather than the using the file
name given to the linker.

Sincerely, I didn’t understand the need of this parameter when generating the shared library 😦

After that, we may move this library to a convenient place, for example /opt/lib

mv libctest.so.1.0 /opt/lib

Then, we can create symbolic links to the major versions of the library, so when someone links to libctest.so or libctest.so.1 they’re using the latest version.

ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so
ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so.1

Using dynamic libraries

To link your program with a dynamic library, you have to inform the location of the library with the parameter -L. Also you specify which libraries you are linking to like this: if you pass the -l<name> parameter, it will link to lib<name>.so object.

gcc -Wall -L/opt/lib prog.c -lctest -o prog

This library is not included in the executable, it will be linked during the run time of your program.

If you try to run the above program right now, probably you’ll receive:

./prog error while loading shared libraries: libpthread_rt.so.1: cannot open shared object file: No such file or directory

Using ldd, we can check the dependencies of our program. Indeed, there’s a missing reference for libctest.so.1.

	linux-gate.so.1 =>  (0x0040a000)
	libctest.so.1 => not found
	libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x00cb5000)
	/lib/ld-linux.so.2 (0x00ecf000)

To resolve this, we can inform the linker where to find the shared library, editing the environment variable LD_LIBRARY_PATH


Alternatively, one can add this path to the configuration file at /etc/ld.so.conf. In Ubuntu this file refers to other configurations files in the directory ld.so.conf.d :

include /etc/ld.so.conf.d/*.conf

So, I just created a file ‘mylocallib.conf’ at this directory containing:


After this, we need to run ldconfig so that this edit take effect.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s