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

Keywords: Qt Embedded system

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.12.6

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 copy the dynamic library. dll file to the same directory of the executable exe 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, typedef needs to be defined before #include < VLC / VLC. H >__ int64 ssize_ t; Otherwise, a bunch of undefined errors may be reported. Typedef can be directly__ int64 ssize_ t; Write the sentence above #include < VLC / VLC. H >.

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) . mode 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
Only opencv 2.x has X86 libraries, and only X64 libraries have been available since 3.X. it is very convenient to add only one library for 3.X version compilation; The following code example is the opencv3.4.x library used. 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

Posted by LeeRoy8888 on Thu, 25 Nov 2021 14:07:29 -0800