Click on the above“Beginner’s Guide to Vision”, select to add aBookmark or “Top”
Essential Content, Delivered First
Essential Content, Delivered First

“This article introduces how to generate library files under the Linux system and how to write header files to use the library functions.”
We know that during the compilation of a C++ project, files containing the main() function will generate an executable program. Code that does not contain the main() function is generally called by other programs, so we can package them into a “thing”, which is a library.
Libraries are generally a collection of many programs and algorithms. For example, the OpenCV library contains many algorithms related to computer vision, and the Eigen library provides many algorithms for matrix algebra calculations.
Let’s take a simple C++ file as an example to demonstrate how to write a library.
Create a new folder named cppSpace in the root directory, and within that folder, create a file named libHelloWorld.cpp:
// Library file without main() function#include <iostream>using namespace std;void printHello(){ cout << "Hello world!" << endl;}
This library file is quite simple and only provides a printHello() function, which outputs a message when called.
According to the principles of CMake, we add the following instructions in the CMakeLists.txt file:
# The instruction syntax format: add_library( program_name source_file ) add_library( hello libHelloWorld.cpp )
This command tells CMake that we want to compile this file into a library named “hello”. Next, use CMake to compile this project, entering the following in the terminal:
cd buildcmake ..make

After running the above commands, a file named libhello.a will be generated in the directory, which is our library. This library is a static library, which generates a copy each time it is called, taking up more space. In contrast, a shared library only produces one copy during multiple calls, saving space. To create a shared library, we add the following instruction in the CMakeLists.txt file:
add_library( hello_shared SHARED libHelloWorld.cpp )
By compiling the project with CMake again, we will obtain the file libhello_shared.so, which is the shared library file.

If we only have these two formats of files, we won’t know what the functions inside are or how to call them. To allow users to utilize these library functions, we need to write a header file that specifies what is included in the library. For users, as long as they declare this header file in their program, they can call this library.
Header files are important directives at the beginning of C++ programs. Below is an example of writing the header file for libhello.a, explaining its writing and usage process.
In the cppSpace folder, create a header file named libHelloWorld.h:
#ifndef LIBHELLOWORLD_H_#define LIBHELLOWORLD_H_// Macro definition to prevent redefinition errors when including the header filevoid printHello(); // Declare the included library function#endif
We have written our header file libHelloWorld.h. Next, we will write an executable program useHello.cpp to call it:
#include "libHelloWorld.h" // Declare the header fileint main(){ printHello(); return 0;}
To successfully call this library function, we also need to link to the library file libhello.a during compilation, which involves adding the following instructions in the CMakeLists.txt file:
# Compile useHello.cpp normallyadd_executable( useHello useHello.cpp )# Link to the library file (Note! Add the library file name, not the header file name)target_link_libraries( useHello hello )
Thus, the file useHello.cpp can use the library functions from hello.a.
In conclusion, the writing and usage process of library files and header files has been introduced.
Download 1: OpenCV-Contrib Extension Module Chinese Version Tutorial
Reply in the "Beginner's Guide to Vision" public account: Extension Module Chinese Tutorial to download the first Chinese version of the OpenCV extension module tutorial available online, covering installation of extension modules, SFM algorithms, stereo vision, object tracking, biological vision, super-resolution processing, and more than twenty chapters of content.
Download 2: Python Vision Practical Project 52 Lectures
Reply in the "Beginner's Guide to Vision" public account: Python Vision Practical Project to download 31 visual practical projects including image segmentation, mask detection, lane line detection, vehicle counting, eyeliner addition, license plate recognition, character recognition, emotion detection, text content extraction, face recognition, etc., to help quickly learn computer vision.
Download 3: OpenCV Practical Projects 20 Lectures
Reply in the "Beginner's Guide to Vision" public account: OpenCV Practical Projects 20 Lectures to download 20 practical projects based on OpenCV to advance OpenCV learning.
Group Chat
Welcome to join the public account reader group to exchange ideas with peers. Currently, there are WeChat groups for SLAM, 3D vision, sensors, autonomous driving, computational photography, detection, segmentation, recognition, medical imaging, GAN, algorithm competitions, etc. (These will gradually be subdivided). Please scan the WeChat ID below to join the group, and note: "Nickname + School/Company + Research Direction", for example: "Zhang San + Shanghai Jiao Tong University + Visual SLAM". Please follow the format; otherwise, you will not be approved. After successfully adding, you will be invited to related WeChat groups based on your research direction. Please do not send advertisements in the group; otherwise, you will be removed. Thank you for your understanding~