Method for adding external third library and header file path in QtCreator (. pro file)

Posted by mingwww on Tue, 11 Jan 2022 10:50:02 +0100

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

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


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();

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
    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 \

Little knowledge: opencv library for mingw compiler can be downloaded here:

Mode 2: for MSVC compiler – 64 bit Opencv official website address: 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\