Jun 9, 2016

C Programming #64: How to write a dynamic library

C Programming #64: How to Write a dynamic library

This article covers how to write a dynamic library. It would be good idea to go through C Programming #63: How to write a static library so that comparison be made to dynamic. Same library created in C Programming #63: How to write a static library would be created but this time linked dynamically. First fact.c is to compiled with -fPIC option which will generate object file which position independent code.

$ gcc -c -fPIC fact.c -o fact.o

nm-ing on fact.o we get

$ nm fact.o
0000000000000000 T factorial
                 U _GLOBAL_OFFSET_TABLE_

Static library can be created of the above object file

$ gcc -shared -o libfact.so fact.o

Running a file command on libfact.so we would get

$ file libfact.so 
libfact.so: ELF 64-bit LSB shared object, x86-64, \
version 1 (SYSV), dynamically linked, \
BuildID[sha1]=300cf85127a20193ce44f4e93d250413aa64ae10, \
not stripped

Which clearly states that libfact.so is a dynamically linked library. Now main application app.c can be linked to libfact.so as follows

$ gcc -L. -Wl,-rpath=. -o app app.c -lfact
  • -L option tells the location search the library while compiling.
  • -rpath options tells the location to search library while loading binary

Now running nm, ldd and file on app we get

$ file app
app: ELF 64-bit LSB executable, x86-64, \
version 1 (SYSV), dynamically linked, \
interpreter /lib64/ld-linux-x86-64.so.2, \
for GNU/Linux 2.6.32, \
BuildID[sha1]=f8e00e246c64ef10bbc262f79449e842df428b65, \
not stripped

$ ldd app
        linux-vdso.so.1 =>  (0x00007ffeb4f5e000)
        libfact.so => ./libfact.so (0x00007faeb00d9000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007faeafced000)
        /lib64/ld-linux-x86-64.so.2 (0x000055912c135000)
$ ls -l app
-rwxrwxr-x   1 hsk            hsk          8640 2016-06-09 17:49 app

You can clearly observe now that it is dynamically linked library, linked to libfact.so and its size is comparatively small to statically linked.


  1. In case of static linked factorial symbol was compiled and defined in binary app, while in the dynamically linked factorial symbols is undefined in app, It will get the definition when load the app.
  2. Statically linked binary and huge (here app was 909144) but dynamically linked are smaller (here app is 8640).
  3. Changing the factorial function would need the whole binary to be compiled in case of statically linked, while in case of dynamically linked we can only compile libfact.so.
  4. If for some reason in case of dynamically linked the library gets deleted then the app wont run.
$ rm libfact.so
$ ./app
app: error while loading shared libraries: \
libfact.so: cannot open shared object file: \
No such file or directory

Hope this gives the fair idea about statically linked and dynamically linked library, It would be good idea to revisit with this knowledge the following article - C Programming #34: Journey from source code to executable

No comments :

Post a Comment