Static library and Dynamic library C++

Agenda

1) Static Library vs Dynamic Library

2) Create and use static library

3) Create and use Dynamic library


A library is a collection of pre-compiled code that can be re-used by programs. There are 2 types of libraries: static library and dynamic library.


1) Static Library vs Dynamic Library

A static library (or archive) contains code that is linked to users’ programs at compile time. The executable file generated keeps its own copy of the library code.

A dynamic library (or shared library) contains code designed to be shared by multiple programs. The content in the library is loaded to memory at runtime. Each executable does not maintain its replication of the library.




2) Create and use static library

Linux -> .a (archive file)
Windows -> .lib file

[Create a static library]:

The process involves 2 steps. 

STEP-1: Generate the object file(s).

    E.g: g++ -c my_code.cpp -o my_code.o

STEP-2: 

[LINUX] involves using ar (a Linux archive utility tool) to create the library file.

ar cr libmy_code.a my_code.o

The “cr” flag is to indicate creating a new static library file. It is followed by the output file name first as a request. Notice the name of output is “libmy_math.a”. It is a convention to name the file libXXX.a in Linux, please always do it. When the library is used, the command line tool actually relies on this convention for the linker to work properly. 

[WINDOWS] Use visual studio.


[Use a static library]:

Now we want to use the static library file. One way is to put the file together with other object files in the g++/gcc linking command.
g++ main.o libmy_code.a -o a.out

Another method more often used is to explicitly specify library path using (-L) and library name (-l):

g++ main.o -L. -lmy_code -o a.out

This tells the compiler to look for libraries in path (.) with name libmy_math.a. Notice here we use -lmy_math. The linker will treat this as specifying file name libmy_math.a (Remember the naming convention for creating library we just talked about). 



3) Create and use Dynamic library

Linux -> .so file
Windows -> .dll file

[Create a dynamic library]:


Let’s use the same sample code and instead create a dynamic library this time. Here is the command:
g++ -shared -o libmy_code.so my_code.o
The “-shared” flag instructs to generate a shared library. Again, the output file naming convention libXXX.so is a must and will be utilized by the linker later.

[Use a dynamic library]:

Similar to static libraries, we have 2 ways to use it. 
1. Put it as an input to the linker/compiler. 
2. Explicitly specify library location (-L) and name (-l):
g++ main.o libmy_code.so -o a.out
# OR:
g++ main.o -L. -lmy_code -o a.out

Simple, right? Let’s run it:

./a.out

./a.out: error while loading shared libraries: libmy_code.so: cannot open shared object file: No such file or directory 

Oops, we got an error (with either executable generated). The runtime is trying to find a shared library named libmy_math.so but can’t. What happened and how to fix it?

Turns out the user must provide hints to the executable or OS for the runtime to find the shared library (libmy_math.so). There are 2 ways in Linux:

1) Append the shared library path to the environment variable LD_LIBRARY_PATH.

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/cpp_tutorial/static_library

2) Use -rpath flag to specify the shared library path when building the executable.

g++ main.o -L. -lmy_code -o a.out -Wl,-rpath,/home/cpp_tutorial/static_library

Here “-Wl flag” means what follows it (which is a comma-separated list of flags) will be are passed to the linker. In this case, “-rpath /home/cpp_tutorial/static_library” is passed to the linker. The linker inserts this path information to the executable’s (a.out) own search path. This also works.

Compare the 2 methods, modifying LD_LIBRARY_PATH involves changing global variables which affects all programs. Using -rpath is usually a preferred way because it is a local change and does not alter behaviors of other executables. To know more about how shared library search path works, you can read Shared Libraries: Understanding Dynamic Loading.



Summary

There are 2 types of libraries: static library and dynamic(shared) library.

Static libraries are copied into the executable at compile time.

Dynamic libraries are not copied, but loaded and linked at runtime.

How to create static/dynamic libraries and use them.

For dynamic libraries, there are 2 ways to specify a library search path: using LD_LIBRARY_PATH or -rpath.

Comments

Popular posts from this blog

KMP Algorithm: Pattern Searching in Text

Z-Function Algorithm: Substring Search

Back of the envelope estimations