Building and using shared libraries

On this page I will use a simple example on how to use shared libraries on Linux in C++ and using the GNU g++ compiler. You will be able to compile your library and application separately.


In Windows we have DLLs, in Linux/Unix we have shared libraries. It's the same!

However, you can't specify your library on runtime easily. It is possible though, using dlopen(), but that's not really easy.


A very simple UML model about the design in this example.

UML model

Our file structure will look like this:



Building a shared library libfoo


#ifndef LIBFOO_H
#define LIBFOO_H

class Foo {
        int bar() const;



#include "foo.h"


int Foo::bar() const {
        return 10;

Now, in the folder foo/ run the following command:

g++ -shared -fPIC foo.cpp -o

The options explained:

  • -shared—to create a shared object file (shared library)
  • -fPIC—about memory management. In short: memory addresses are mapped independent of the location.
  • foo.cpp—the C++ file(s) to compile
  • -o—instead of outputting to ''a.out'', use a sensible name.

Now check if you can see if all methods are included:

$ nm foo/
0000000000200e20 a _DYNAMIC
00000000000005ec T _ZN3FooC1Ev
00000000000005ec T _ZN3FooC2Ev
00000000000005f6 T _ZNK3Foo3barEv
00000000000005c0 t frame_dummy

These are all very cryptic names, called symbols. To make it human readable, use c++filt:

$ echo _ZN3FooC1Ev | c++filt 
$ echo _ZNK3Foo3barEv | c++filt 
Foo::bar() const


In case you don't have the utilities c++filt or nm then install the package binutils on Debian/Ubuntu and -derivatives.

Obviously, our shared library is created successfully. Now let's move on to how to use it.

Using shared libraries


#include <iostream>
#include "../foo/foo.h"

int main(){
        Foo myfoo;
        std::cout << "Output of" << << std::endl;

Note that we use the foo.h file from the foo/ folder as we need it to compile the qux application.

Now we can compile the qux application as follows:

g++ -lfoo -L ../foo/ main.cpp -o qux

The options explained:

  • -lfoo—use library foo, means look for shared object files with the name*
  • -L ../foo/—when trying to link object files look in the directory ../foo/.
  • main.cpp—the source file to compile.
  • -o qux—output to file qux instead of a.out.

Just when you think you're done and you try to run the program...

$ ./qux 
./qux: error while loading shared libraries: cannot open shared object file: No such file or directory

Apparently it is not able to find our file on runtime! To debug it, we can use ldd to find out what's going on.

$ ldd qux =>  (0x00007fffa9dff000) => not found => /usr/lib/x86_64-linux-gnu/ (0x00007f007578d000) => /lib/x86_64-linux-gnu/ (0x00007f0075507000) => /lib/x86_64-linux-gnu/ (0x00007f00752f1000) => /lib/x86_64-linux-gnu/ (0x00007f0074f5d000)
        /lib64/ (0x00007f0075abd000)

Apparently it doesn't know where it can find This can be solved by specifying the correct path for the linker as follows:

$ LD_LIBRARY_PATH=../foo/ ./qux
Output of

Party! We did it! :-D

Why doesn't it know the correct path? We specified it on compile time, right? Yes, that was on compile time, not runtime. In real installations we put the shared library in system paths like /usr/lib and in that case we don't need to specify the LD_LIBRARY_PATH variable.

Things to keep in mind

  • In order to be able to build any application in Ubuntu/Debian, make sure you have installed the build-essential package.
  • When you want to use a shared library you'll need the corresponding header files foo.h to be able to use it.
  • Also, when distributing or sharing shared libraries with others, you'll need to share the header files as well.
  • If you want to compile against a system library, you will need to install the header files. Usually you'll find them in the -dev packages on Debian/Ubuntu distrubutions.
  • With the method described on this page you can't specify the .so file in runtime.
  • None of the commands given need to be run as root.

See also

Share on: TwitterHacker NewsFacebookLinkedInRedditEmail


comments powered by Disqus

Related Posts





Connect with me on...