top of page

Library in c++


What is library:

A library is a package of code that is meant to be reused by many programs.

There are two types of libraries:

1) static libraries

2) dynamic(shared) libraries

static libraries:

A static library (also known as an archive) consists of routines that are compiled and linked directly into your program. When you compile a program that uses a static library, all the functionality of the static library that your program uses becomes part of your executable. On Windows, static libraries typically have a .lib extension, whereas on linux, static libraries typically have an .a (archive) extension. One advantage of static libraries is that you only have to distribute the executable in order for users to run your program. Because the library becomes part of your program, this ensures that the right version of the library is always used with your program. Also, because static libraries become part of your program, you can use them just like functionality you’ve written for your own program. On the downside, because a copy of the library becomes part of every executable that uses it, this can cause a lot of wasted space. Static libraries also can not be upgraded easy -- to update the library, the entire executable needs to be replaced.

Steps to create a static library Let us create and use a Static Library in UNIX or UNIX like OS.

1) Create a C++ file that contains functions in your library.

/* Filename: display.cpp */
#include <iostream> 
void display(void) 
{ 
  Std :: cout << “in Display member function”; 
}

2) Create a header file for the library

/* Filename: display.h */
void display(void);

3) Compile library file

G++ -c display.cpp -o display.o

4) Create static library. This step is to bundle multiple object files in one static library.

ar rcs display.a lib_display.o

5) Now our static library is ready to use.

Let us create a driver program that uses above created static library:

1) create a c++ file with main function.

/* filename: driver.cpp */
#include "display.h" 
void main()  
{ 
  display(); 
}

2) compile the driver program.

g++ -c driver.cpp –o driver.o

3) Link the compiled driver program to the static library. Note that -L. is used to tell that the static library is in current folder (See this for details of -L and -l options).

g++ -o driver driver.o –L. –ldisplay

4) Run the driver program

./driver

dynamic libraries:

A dynamic library (also called a shared library) consists of routines that are loaded into your application at run time. When you compile a program that uses a dynamic library, the library does not become part of your executable -- it remains as a separate unit. On Windows, dynamic libraries typically have a .dll (dynamic link library) extension, whereas on Linux, dynamic libraries typically have a .so (shared object) extension. One advantage of dynamic libraries is that many programs can share one copy, which saves space. Perhaps a bigger advantage is that the dynamic library can be upgraded to a newer version without replacing all of the executables that use it.

Because dynamic libraries are not linked into your program, programs using dynamic libraries must explicitly load and interface with the dynamic library. This mechanism can be confusing, and makes interfacing with a dynamic library awkward. To make dynamic libraries easier to use, an import library can be used.

An import library is a library that automates the process of loading and using a dynamic library. On Windows, this is typically done via a small static library (.lib) of the same name as the dynamic library (.dll). The static library is linked into the program at compile time, and then the functionality of the dynamic library can effectively be used as if it were a static library. On Linux, the shared object (.so) file doubles as both a dynamic library and an import library. Most linkers can build an import library for a dynamic library when the dynamic library is created.

Step to create dynamic(shared) library:

1) Let us work with simple shared library on Linux. Create a file library.cpp with the following content.

/* Filename: library.cpp */
#include <iostream> 
void display(void) 
{ 
  Std :: cout << “in Display member function”; 
}

In library.cpp defined a function display() which will be used by our application code.

2) Compile the file library.cpp file using the following command.

gcc -shared -fPIC -o liblibrary.so library.cpp

The flag -shared instructs the compiler that we are building a shared library. The flag -fPIC is to generate position independent code (ignore for now). The command generates a shared library liblibrary.so in the current working directory. We have our shared object file (shared library name in Linux) ready to use.

Let us create a application program that uses above created dyanamic(shared) library:

1) Create another file application.cpp with the following content.

/* filename: application.cpp */
#include "display.h" 
void main()  
{ 
  display(); 
}

In the file application.cpp we are invoking the function display() which was defined in a shared library.

2) Compile the application.cpp file using the following command.

gcc application.cpp -L /home/prashant.raj/coding/ -llibrary -o sample

The flag -llibrary instructs the compiler to look for symbol definitions that are not available in the current code (signum function in our case). The option -L is hint to the compiler to look in the directory followed by the option for any shared libraries (during link time only). The command generates an executable named as “sample“.

If you invoke the executable, the dynamic linker will not be able to find the required shared library. By default it won’t look into current working directory. You have to explicitly instruct the tool chain to provide proper paths. The dynamic linker searches standard paths available in the LD_LIBRARY_PATH and also searches in system cache (for details explore the command ldconfig).

3) We have to add our working directory to the LD_LIBRARY_PATH environment variable. The following command does the same.

export LD_LIBRARY_PATH=/home/prashant.raj/coding/:$LD_LIBRARY_PATH

4) Run the our application code.

./sample

Difference between static library and dynamic(shared) library:




Comments


bottom of page