1, Foreword
In the process of project development, third-party libraries are often used. The path, header file path, referenced library name, etc. of the third-party library need to be specified in QtCreator project; In addition, you may also need to write a general project to select different libraries for different compiler types and different operating system environments; Then the distinction between these conditions can be implemented logically in the pro project file of QtCreator.
The following describes the common methods of adding libraries and header files. Taking libvlc, ffmpeg, opencv and other common third-party libraries as examples, the compiler selects VS2017(32-bit) and MinGW(32-bit) for testing respectively.
2, Add third party libraries and header files
In the following example, the third-party libraries and header files are placed in the project directory. In the code, use $$PWD to get the current path directly, and then connect to the path of the library directory. The program testing environment is win10(64bit) and Qt5 twelve point six
Note: both left slash and right slash can be supported when adding paths. / font\
2.1 example of referencing libvlc Library

MSVC and MinGW compilers can be used After compiling, you need to use the dynamic library dll file can be copied to the same level directory of exe executable file to run normally.
INCLUDEPATH += $$PWD\VLC\sdk\include #VLC header file contains directory INCLUDEPATH += $$PWD\VLC\sdk\include\vlc\plugins #VLC header file contains directory INCLUDEPATH += $$PWD\VLC\sdk\include\vlc #VLC header file contains directory LIBS +=$$PWD\VLC\sdk\lib\libvlc.lib #Referenced VLC library file LIBS +=$$PWD\VLC\sdk\lib\libvlccore.lib
Small knowledge: when MSVC compiler compiles with libvlc library, it needs to be in #include < VLC / VLC h> Before, define typedef__ int64 ssize_ t; Otherwise, a bunch of undefined errors may be reported. Typedef can be directly__ int64 ssize_ t; Write in #include < VLC / VLC h> The above sentence.
The code references the header files that libvlc needs to contain:
#include <vlc/vlc.h>
libvlc test code: print the version of the library
qDebug()<<"libvlc edition:"<<libvlc_get_version(); //Print result: libvlc version: 3.0.12 Vetinari
2.2 example of referencing ffmpeg Library
(1). Method 1: applicable to mingw and MSVC compilers

INCLUDEPATH+=$$PWD/ffmpeg/include LIBS += -L$$PWD/ffmpeg/lib/ -lavcodec -lavdevice -lavfilter -lavformat -lavutil -lpostproc -lswresample -lswscale DEPENDPATH += $$PWD/ffmpeg/lib
(2). Mode 2: applicable to mingw and gcc

INCLUDEPATH+=$$PWD/ffmpeg/include LIBS+=$$PWD/ffmpeg/bin/av* LIBS+=$$PWD/ffmpeg/bin/sw* LIBS+=$$PWD/ffmpeg/bin/pos*
The code refers to the header files to be included in ffmpeg:
//Declare the header file that references C extern "C" { #include <stdlib.h> #include <stdio.h> #include <string.h> #include <math.h> #include <libavutil/avassert.h> #include <libavutil/channel_layout.h> #include <libavutil/opt.h> #include <libavutil/mathematics.h> #include <libavutil/timestamp.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> #include <libswresample/swresample.h> #include "libavfilter/avfilter.h" #include "libavutil/avassert.h" #include "libavutil/channel_layout.h" #include "libavutil/common.h" #include "libavutil/opt.h" }
ffmpeg test code: print the version of the library
const char *p=av_version_info(); qDebug("%s\n",p);
2.3 reference opencv library example
Method 1: applicable to gcc and mingw
#Path setting of linu platform linux { message('function linu edition') #To add the path of opencv header file, you need to modify it according to your own header file path INCLUDEPATH+=/home/wbyq/work_pc/opencv-3.4.9/_install/install/include\ /home/wbyq/work_pc/opencv-3.4.9/_install/install/include/opencv\ /home/wbyq/work_pc/opencv-3.4.9/_install/install/include/opencv2 LIBS+=/home/wbyq/work_pc/opencv-3.4.9/_install/install/lib/libopencv_* } win32 { message('function win32 edition') #To add the path of opencv header file, you need to modify it according to your own header file path INCLUDEPATH+=C:/OpenCV_3.4.7/OpenCV-MinGW-Build-OpenCV-3.4.7/include \ C:/OpenCV_3.4.7/OpenCV-MinGW-Build-OpenCV-3.4.7/include/opencv \ C:/OpenCV_3.4.7/OpenCV-MinGW-Build-OpenCV-3.4.7/include/opencv2 LIBS+=C:/OpenCV_3.4.7/OpenCV-MinGW-Build-OpenCV-3.4.7/x86/mingw/bin/libopencv_*.dll }
Little knowledge: opencv library for mingw compiler can be downloaded here: https://github.com/huihut/OpenCV-MinGW-Build
Mode 2: for MSVC compiler – 64 bit Opencv official website address: https://opencv.org opencv 2.x has the X86 library, from 3 Since the beginning of X, there are only X64 libraries, 3 X version compilation only needs to add a library, which is very convenient; The following code example is opencv3 For the 4. X library, the MSVC compiler selects VS2017 64bit.
INCLUDEPATH += $$PWD/opencv/build/include\ INCLUDEPATH += $$PWD/opencv/build/include/opencv\ INCLUDEPATH += $$PWD/opencv/build/include/opencv2 LIBS += -L$$PWD/opencv/build/x64/vc14/lib\ -lopencv_world320d