Libraries

C++

Talha Hanif Butt
1 min readFeb 24, 2023

Libraries are collection of symbols or collection of function implementations. Basically multiple object files that are logically connected.

There are 2 types: Static and Dynamic.
Static end with .a while dynamic end with .so

Header file for declaration of a function as .hpp file
.cpp file the main program including .hpp file

If all function definitions are in .hpp file then we are not using the linker which will take up a lot of memory if we have billions of files. One can run out of memory. Build will be slow.

Linker will search for implementations of the functions declared in the .hpp file.

Library is a binary object containing compiled implementation of some methods.

To use a library we need;
a header file .h
compiled library object .a

If we have a main.cpp file including tools.hpp and a tools.cpp file containing the required function definitions then the compilation would be like this:

Compile modules
c++ -std=c++17 -c tools.cpp -o tools.o
Organize modules into libraries
ar rcs libtools.a tools.o
Link libraries
c++ -std=c++17 main.cpp -L . -ltools -o main
Run the code
./main

--

--