Building a shared dynamic library

Dynamic libraries must be compiled in a position-independent way, so that the linker can locate them in any memory location at runtime. To do this, we use the -fPIC parameter to the compiler. To build a dynamic library from the same sources, we run the following:

$ ${CC} -fPIC -g -c hello.c world.c
$ ${CC} -shared -Wl,-soname,libhelloworld.so.1 -o libhelloworld.so hello.o world.o

We now build it indicating where to find the dynamic library (inside libs/) and the include file (inside inc/) to the compiler:

$ ${CC} -o helloworld helloworld.c -l helloworld  -L libs/ -I inc/

Again, we can check the dynamic libraries using readelf, as follows:

$ readelf -d helloworld
Dynamic section at offset 0x6ec contains 25 entries:
Tag Type Name/Value

0x00000001 (NEEDED) Shared library:
[libhelloworld.so.1]

0x00000001 (NEEDED) Shared library: [libc.so.6]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset