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
[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]:
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
[Create a dynamic library]:
g++ -shared -o libmy_code.so my_code.o
[Use a dynamic library]:
g++ main.o libmy_code.so -o a.out
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
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
Post a Comment