Hi, I’m Songxi from Amu LABS, and today I’m going to introduce you to the concept of libraries in Linux.

A brief introduction of the library

What is a library? A library is simply a module. Modules that provide different functions, such as Ceres library, eigen library, PCL library, etc. In essence, a library is a binary form that can execute code and can be loaded into memory. In Liunx system, libraries exist in the form of files, and can be divided into dynamic link library and static link library, referred to as dynamic library and static library. The suffix of static library files is.a, and that of dynamic library files is.so. Libraries, whether dynamic or static, simply provide a variable, function, or class to the caller.

Two, the difference between the library

The difference between static and dynamic libraries is that the code is loaded at different times. The static library will be linked to the object code when the program is compiled. When the object program is running, the library will no longer be needed. It is convenient to transplant, but the volume will become large, wasting space and resources, because all the files related to it will be linked into an executable file, resulting in the size of the executable file. Dynamic libraries are not linked to object code when the program is compiled, but are loaded while the program is running, so the executable file size is smaller.

Most libraries are dynamic, such as eigen, Ceres, etc. This is commonly referred to as dependency. If you need a library in your program, and your environment does not have such a library, you will get an error at compile time: xxxx.h file not found. This is a classic lack of dynamic libraries. To solve this problem, you only need to install the corresponding dynamic library. This problem often occurs when you clone a piece of code, but there is no runtime environment for the code, and when you compile it, the above error will occur. Did you get another trick?

Third, static library creation and use

The procedure for creating a static library in Linux is as follows:

  1. Edit the source file (.c or.cpp file)

  2. Generate object file (.o file) with GCC -c xxx.c or g++ -c xxx.cpp

  3. Use ar command to archive object files and generate static libraries

  4. Write a header file with the static library. The contents of the header file are declarations of functions, variables, or classes that are provided to the caller.

It is important to understand the use of the AR command before the actual creation. The ar command can not only create static libraries, but also modify or extract information from existing static libraries. Its basic usage is as follows:

ar [option] libxxx.a xx1.o xx2.o xx3.o

Libxxx. a is the name of the static library, XXX is the name of the static library, lib is the name of the library, and all libraries under Linux follow this name. Libxxx. a or libxxx.so. Xx1.o xx2.o is the object code file of the static library. There can be more than one. Option Common options are as follows:

-c: Creates a library, regardless of whether the library exists

-s: Creates the index of the target file

-r: Inserts a module into the library. If the inserted module name already exists in the library, it will be replaced. If a module does not exist, it will be saved and will not replace other modules with the same name

-t: displays the object files in the library file. Show only

-tv: displays the target files of the library file. The file name, time, and size are included

-s: displays index tables in static library files

How to create and use a static library

Create a new source file, test.cpp, and copy the following code:

#include

void show_age(int age) { std::cout << “Your age is: ” << age << std::endl; } then enter the command

G++ -c test.cpp ar RCS libshowage.a test.o this generates a static library. To use it, you need to write another source file, demo.cpp. Copy the following code

#include

extern void show_age(int age); // Declare the function to use

int main(int argc, char** argv) { show_age(18); std::cout << “Hi” << std::endl; } CMakeLists. TXT file

Cmake_minimum_required (VERSION 3.0) project (demo)

add_executable(demo demo.cpp) target_link_libraries(demo -L.. -lshowage) where the -l parameter indicates where to find the library, (..) Indicates the upper level directory. -l specifies the specific libraries, lib and. A do not need to be written out, the compiler will automatically look for the libshowage.a file. This is why libraries are named starting with lib.

Fourth, the creation and use of dynamic library

On Linux, the path for storing dynamic libraries is /usr/lib. In Linux, the default link is to link the dynamic library first. If both static and dynamic libraries exist, the dynamic library will be linked unless specified. This helps save space.

Similarly, we create a dynamic library, test.cpp. Same code, no changes. You only need to modify the command that generates the dynamic library. The command is as follows:

G++ test. ccp-fpic-shared-o libshowage. So-shared indicates that a shared library is generated

-fPIC Indicates that address – independent code is used. PIC stands for Position Independent Code. This parameter must be added when compiling shared libraries on Linux, otherwise an error will be reported when linking. Because shared library files may be loaded in different locations by different processes, if the instructions in the shared object use absolute addresses, external module addresses, etc., then the address of the shared object needs to be changed when it is loaded, so that multiple processes can not share a piece of physical memory.

Write a function that calls the library, demo.cpp. The content remains the same. Cmakelists.txt is also unchanged.

Five, multiple files to generate a dynamic library

Create the test1.cpp file and copy the following code

#include

void show_age_1(int age) { std::cout << “This is lib_1: ” << age << std::endl; } Create the test2.cpp file and copy the following code

#include

void show_age_2(int age) { std::cout << “This is lib_2: ” << age << std::endl; } create a dynamic library

G++ test1.cpp test2.cpp-fpic-shared-o libshowage.so if you want to create a dynamic library from multiple files, you must not declare functions, classes, or variables with the same name. The compiler does not know which source file to invoke.

Create the main. CPP file and, using the library, copy the following code

#include

// declare the function extern void show_age_1(int age); extern void show_age_2(int age);

int main(int argc, char** argv) { show_age_1(18); show_age_2(36); } the cmakelists.txt file is as follows

Cmake_minimum_required (VERSION 3.0) project (demo)

add_executable(main main.cpp) target_link_libraries(main -L.. -lshowage) when we run, we will find this error

./main: error while loading shared libraries: libshowage.so: cannot open shared object file: At compile time, we tell the compiler where to find the library, but at run time, because the library is a dynamic library, its code is not linked to the target code, so at run time, it will tell us that the library can not be found.

Therefore, simply place the dynamic library on the default path or tell it to specify the path.

In Linux, libraries are typically placed in one of three places. /usr/local/lib = /usr/local/lib = /usr/local/lib = /usr/local/lib = /usr/local/lib = /usr/local/lib = /usr/local/lib = /usr/local/lib

Therefore, to place our library in /usr/local/lib, type the following command

Sudo cp ~/C++/demo/libshowage.so /usr/local/lib

Sudo ldConfig so that when we run it again, we don’t get an error. Keep in mind that every time a dynamic library changes in the above directory, you need to enter this command to update, otherwise it still cannot be identified.

Install the missing dynamic library via APT

When we clone the code from Github, it often appears that the xxx.h file does not exist, or No such file or directory, during compilation. This is essentially a mismatch between your environment and someone else’s and a lack of libraries. The general solution is to copy the missing header file to Baidu and check which library the header file is from. Then open the terminal and enter the command

Sudo apt install libxxx-dev

If it is a custom dynamic library, it is usually explained in the READme document.

Similarly, when you contribute open source code to the open source community, be sure to inform the environment in which you are running and the libraries you need.

Seven,

I believe that through the introduction of this article, readers have a simple understanding of the library, in the future work and study process, encountered similar problems, but also have the idea and method to solve the problem.

  • End –

With the rapid development of technology, AMu Lab will keep up with the pace of technology and constantly recommend the latest technology and hardware in the robot industry to everyone. The greatest value of our training is to see our trainees make rapid progress in technology. If you are in the robotics industry, please follow our official account, we will continue to publish the most valuable information and technology in the robotics industry. Amu Lab is committed to providing open source software and hardware tools and course services for robot r&d to make r&d more efficient!