Record the configuration of the tripartite library environment

Record how to set up the three-party library in the development environment, so that you can browse and solve problems quickly at any time later. It is mainly a method that I personally think is easier to use in actual development.

analyze

The difference between dynamic library and static library must be understood first! Such as .lib .dll .a .so otherwise it will affect the understanding below .
No matter what, the call to the three-party library is nothing more than two steps:
1. The path of the header file and the library path of the three-party library can be found in the source code. Setting errors will cause various compilation errors to report that the definition cannot be found.
2 Executing the program can find the linked library path. If the setting is wrong, it will fail to run and end abnormally

Compile-time library settings

The first step is relatively simple. All major IDEs have fixed methods, and there is no difference between Windows and Linux environment methods. The library can be included, take the ffmpeg library as an example, and other libraries by analogy:

The Qt way:

//库路径
win32: LIBS += /home/zxt/ffmpeg-4.3.4/lib/ -lavcodec
win32: LIBS += /home/zxt/ffmpeg-4.3.4/lib/ -lavdevice
win32: LIBS += /home/zxt/ffmpeg-4.3.4/lib/ -lavfilter
win32: LIBS += /home/zxt/ffmpeg-4.3.4/lib/ -lavformat
win32: LIBS += /home/zxt/ffmpeg-4.3.4/lib/ -lavutil
win32: LIBS += /home/zxt/ffmpeg-4.3.4/lib/ -lpostproc
win32: LIBS += /home/zxt/ffmpeg-4.3.4/lib/ -lswresample
win32: LIBS += /home/zxt/ffmpeg-4.3.4/lib/ -lswscale
//头文件路径
INCLUDEPATH += /home/zxt/ffmpeg-4.3.4/include
DEPENDPATH += /home/zxt/ffmpeg-4.3.4/include

The CMake way:

//Add FFmpegLib Path 
INCLUDE_DIRECTORIES(/home/zxt/ffmpeg-4.3.4/include)
LINK_DIRECTORIES(/home/zxt/ffmpeg-4.3.4/lib)
link_libraries(-lavcodec -lavformat -lavfilter -lavutil -lswscale -lswresample -lavdevice -lpostproc)

VS mode:
In the Windows environment project property page, additional include directory: set header file path; additional library directory, additional dependencies: set library path

The above settings are completed and the compilation can pass. The key point is how to make the execution program find the link library and run normally.

Runtime library settings

All in all, let the executable program link to the dependent third-party library, set the library path to know where the third-party library to be linked is!

Windows environment

Method 1:
Generally, the dependent dll files can be found directly in the same directory as the execution program (normal practice);
Method 2:
Set the dll path as a global environment variable (this method is not recommended, if there is a library, it is not recommended to set a global variable Too suitable, but frequently used libraries can be used, such as ffmpeg library, I often use the command line to test)

Linux environment

Due to system differences, it is invalid to place the so library in the same directory as the execution program . The simplest method cannot be used but is applicable to the Linux environment. The only solution is to set global variables.

Method 1:
Open the terminal, import the required so library path and then run the program. This is only valid for the current terminal, and it needs to be reset after closing and opening a new terminal.

//导入so库所在路径
export LD_LIBRARY_PATH=/home/zxt/ffmpeg-4.3.4/lib
//执行
sudo ./test

Method 2:
In the configuration file /etc/ld.so.conf, add the so library path used, and then don’t forget to sudo ldconfig to take effect.
According to the following figure, you can directly add a line to the configuration file, the content is the so library path. You can also create a new conf file in the /etc/ld.so.conf.d folder, the content is the so path, which can be automatically indexed.
so library

Method 3:
Configure the path parameters when compiling the program. There is this method, but I don’t like it and don’t record it.

In addition, if there are multiple different version libraries in the environment, you can check which library is linked by ldd+program name

Guess you like

Origin blog.csdn.net/T__zxt/article/details/127005192