How to add PcapPlusPlus library to a Qt Widgets project

100 Views Asked by At

I am working on Qt projects and will use PcapPlusPlus library for reading a PcapNG file.

I am using Windows and MSVC 2019 64 bit, and downloaded this library for my project: pcapplusplus-23.09-windows-vs2022-x64-release.

Pcap++ library doesn't work alone. So, It needs dependency libraries. I download Npcap and WinPcap SDK. I included .lib libraries and include directory to .pro file.

I edited the .pro file for adding PcapPlusPlus, Npcap, WinPcap libraries:

INCLUDEPATH += \
    $$PWD/external/pcapplusplus/include/pcapplusplus \
    $$PWD/external/pcapplusplus/include \
    $$PWD/external/WpdPack/Include \
    $$PWD/external/WpdPack/Include/pcap \
    $$PWD/external/npcap/Include \
    $$PWD/external/npcap/Include/pcap \

LIBS += \
    -L$$PWD/external/pcapplusplus/lib -lPcap++ -lCommon++ -lPacket++ \
    -L$$PWD/external/WpdPack/Lib/x64 -lPacket -lwpcap \
    -L$$PWD/external/npcap/Lib/x64 -lPacket -lwpcap \

DEPENDPATH += \
    $$PWD/external/pcapplusplus/include/pcapplusplus \
    $$PWD/external/pcapplusplus/include \
    $$PWD/external/WpdPack/Include \
    $$PWD/external/WpdPack/Include/pcap \
    $$PWD/external/npcap/Include \
    $$PWD/external/npcap/Include/pcap \

And included the library using:

#include "IPv4Layer.h"
#include "Packet.h"
#include "PcapFileDevice.h"

There are pcapng.h and pcapng.cpp files for reading data bytes from PcapNG file with the library.

When I declare an object like this:

pcpp::PcapFileReaderDevice reader(R"(C:\Users\abc.xyz\Desktop\1_packet.pcap)");

There is no error, but when I build the project, I got 2 errors:

Common++.lib(IpAddress.obj):-1: 
error: LNK2019: unresolved external symbol __imp_inet_pton referenced in function 
"public: __cdecl pcpp::IPAddress::IPAddress(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" 
(??0IPAddress@pcpp@@QEAA@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)

Common++.lib(IpAddress.obj):-1: 
error: LNK2019: unresolved external symbol inet_ntop referenced in function 
"public: bool __cdecl pcpp::IPv4Address::matchSubnet(class pcpp::IPv4Address const &,class pcpp::IPv4Address const &)const " 
(?matchSubnet@IPv4Address@pcpp@@QEBA_NAEBV12@0@Z)

I downloaded a precompiled library and included dependency libraries to the Qt project. But I still get an error.

1

There are 1 best solutions below

3
hyde On

The LIBS should be

LIBS += -L$$PWD\pcapplusplus\lib -lpcapplusplus

Or something like that. That is, add library search paths with -L, libraries with -l (the library "base name", no prefix or suffix).