This is the 17th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021″

One, foreword

In the project development process, often need to use the third party library, need to specify the path of the third library in QtCreator project, header file path, reference library name and so on; You may also need to write generic projects to select different libraries for different compiler types, bits, and operating system environments; Then the distinction of these conditions can be written in the pro project file of QtCreator logic implementation.

The following describes the common methods of adding libraries and header files. Take libvlc, FFmPEG, OpencV and other common third-party libraries as examples. The compiler selects VS2017(32-bit) and MinGW(32-bit) respectively for testing.

Add third-party libraries and header files

$$PWD = $$PWD = $$PWD = $$PWD = $$PWD = $$PWD The program test environment is Win10 (64bit), Qt5.12.6

Note: When adding a path, the left slash and right slash are supported. / \

2.1 Reference to the libvlc library example

MSVC and MinGW compilers can be used. After compiling, you need to copy the used dynamic library. DLL file to the same directory as the executable file exe to run normally.

INCLUDEPATH += $$PWD\VLC\ SDK \include #VLC header directory INCLUDEPATH += $$PWD\VLC\ SDK \include\ VLC\ plugins #VLC header directory INCLUDEPATH +=$$PWD\VLC\ SDK \include\ VLC #VLC header file contains the directory LIBS +=$$PWD\VLC\ SDK \lib\libvlc.lib # reference VLC library file LIBS +=$$PWD\VLC\sdk\lib\libvlccore.libCopy the code

Typedef __int64 ssize_t before #include < VLC /vlc.h>; Otherwise you might get a bunch of undefined errors. Typedefs __int64 ssize_t; #include < VLC /vlc.h>

The code to reference libvlc includes the following header:

#include <vlc/vlc.h>
Copy the code

Test code for libvlc: print library version

qDebug() < <"Libvlc version."<<libvlc_get_version(a);// Print result: libvlc version: 3.0.12vetinari
Copy the code

2.2 Reference ffMPEG library examples

(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
Copy the code

(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*
Copy the code

Ffmpeg header file used to reference ffmpeg:

// Declare a 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"
}
Copy the code

Ffmpeg test code: print library version

const char *p=av_version_info(a);qDebug("%s\n",p);
Copy the code

2.3 Examples of Referencing opencV Libraries

Mode 1: suitable for GCC and MINGW

#linu platform path Settings
linux {
message('Run linu version'INCLUDEPATH+=/home/wbyq/work_pc/opencv3.49./_install/install/include\
             /home/wbyq/work_pc/opencv3.49./_install/install/include/opencv\
             /home/wbyq/work_pc/opencv3.49./_install/install/include/opencv2
 
LIBS+=/home/wbyq/work_pc/opencv3.49./_install/install/lib/libopencv_*
}
 
win32
{
    message('Running the Win32 version'INCLUDEPATH =C:/OpenCV_3 INCLUDEPATH =C:/OpenCV_34.7./OpenCV-MinGW-Build-OpenCV3.47./include \
                 C:/OpenCV_34.7./OpenCV-MinGW-Build-OpenCV3.47./include/opencv \
                 C:/OpenCV_34.7./OpenCV-MinGW-Build-OpenCV3.47./include/opencv2
    LIBS+=C:/OpenCV_34.7./OpenCV-MinGW-Build-OpenCV3.47./x86/mingw/bin/libopencv_*.dll
}
Copy the code

OpenCV libraries for mingw compilers can be downloaded here: github.com/huihut/Open…

Opencv 2. X only X86 library, from 3. x only X64 library, 3.X version only need to add a library is very convenient; The following code example is a library using opencv3.4.x, and 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
Copy the code