Search code examples
c++qtqt-creatorqmakepcapplusplus

Configure Qt project (.pro file) to use PcapPlusPlus


I want to use Pcap++ inside my Qt project. I've followed steps in the installation & introduction guide of PcapPlusPlus, but there are still unknowns:

  • How do I implement that in my .pro file : include ../../../Dist/mk/PcapPlusPlus.mk
  • And that:
all: 
    g++.exe $(PCAPPP_INCLUDES) -c -o main.o main.cpp
    g++.exe $(PCAPPP_LIBS_DIR) -static-libgcc -static-libstdc++ -o Tutorial-LiveTraffic.exe main.o $(PCAPPP_LIBS)

I've already add my LIBS and my INCLUDEPATH like that :

LIBS += -L/usr/local/lib/libCommon++.a -lCommon++
LIBS += -L/usr/local/lib/libPacket++.a -lPacket++
LIBS += -L/usr/local/lib/libPcap++.a -lPcap++
LIBS += -lpcap

INCLUDEPATH += /usr/local/include/pcapplusplus

And to give you an example, when I try to call code as follow in my main function:

#include <IPv4Layer.h>
#include <Packet.h>
#include <PcapFileDevice.h>


int main(int argc, char** argv) {

    pcpp::PcapFileReaderDevice reader("the/path/example-app/1_packet.pcap");

    return 0;
}

I got a lot of errors like this :

/tmp/cirrus-ci-build/PcapPlusPlus/Common++/header/Logger.h:173: error : undefined reference to `pcpp::LoggerPP::LoggerPP()'

Solution

  • The idea is to convert the .mk to an appropriate code that qmake understands, for example in my case the .mk is:

    PCAPPLUSPLUS_HOME := /home/developer/PcapPlusPlus-20.08
    
    ### COMMON ###
    
    # includes
    PCAPPP_INCLUDES := -I$(PCAPPLUSPLUS_HOME)/Dist/header
    
    # libs dir
    PCAPPP_LIBS_DIR := -L$(PCAPPLUSPLUS_HOME)/Dist
    
    # libs
    PCAPPP_LIBS := -lPcap++ -lPacket++ -lCommon++
    
    # post build
    PCAPPP_POST_BUILD :=
    
    # build flags
    PCAPPP_BUILD_FLAGS :=
    
    ifdef PCAPPP_ENABLE_CPP_FEATURE_DETECTION
        PCAPPP_BUILD_FLAGS += -DPCAPPP_CPP_FEATURE_DETECTION -std=c++11
    endif
    
    ifndef CXXFLAGS
    CXXFLAGS := -O2 -g -Wall
    endif
    
    PCAPPP_BUILD_FLAGS += $(CXXFLAGS)
    ### LINUX ###
    
    # includes
    PCAPPP_INCLUDES += -I/usr/include/netinet
    
    # libs
    PCAPPP_LIBS += -lpcap -lpthread
    
    # allow user to add custom LDFLAGS
    PCAPPP_BUILD_FLAGS += $(LDFLAGS)
    

    So using the previous information build the following .pro:

    TEMPLATE = app
    CONFIG += console c++11
    CONFIG -= app_bundle
    CONFIG -= qt
    
    SOURCES += \
            main.cpp
    
    PCAPPLUSPLUS_HOME = /home/developer/PcapPlusPlus-20.08
    
    PCAPPP_INCLUDES = $${PCAPPLUSPLUS_HOME}/Dist/header
    PCAPPP_LIBS_DIR = -L$${PCAPPLUSPLUS_HOME}/Dist
    PCAPPP_LIBS = -lPcap++ -lPacket++ -lCommon++
    PCAPPP_POST_BUILD =
    PCAPPP_BUILD_FLAGS =
    defined(!PCAPPP_ENABLE_CPP_FEATURE_DETECTION){
        PCAPPP_BUILD_FLAGS += -DPCAPPP_CPP_FEATURE_DETECTION -std=c++11
    }
    defined(!CXXFLAGS){
        CXXFLAGS = -O2 -g -Wall
    }
    PCAPPP_BUILD_FLAGS += $${CXXFLAGS}
    PCAPPP_INCLUDES += -I/usr/include/netinet
    PCAPPP_LIBS += -lpcap -lpthread
    PCAPPP_BUILD_FLAGS += $${LDFLAGS}
    
    LIBS += $${PCAPPP_LIBS}
    INCLUDEPATH += $${PCAPPP_INCLUDES}
    DEPENDPATH += $${PCAPPP_LIBS_DIR}