配置 Qt 项目(.pro 文件)以使用 PcapPlusPlus
Posted
技术标签:
【中文标题】配置 Qt 项目(.pro 文件)以使用 PcapPlusPlus【英文标题】:Configure Qt project (.pro file) to use PcapPlusPlus 【发布时间】:2020-10-02 14:04:03 【问题描述】:我想在我的 Qt 项目中使用 Pcap++。 我已经按照PcapPlusPlus的安装和介绍指南中的步骤进行了操作,但仍有未知数:
如何在我的 .pro 文件中实现它:include ../../../Dist/mk/PcapPlusPlus.mk
还有:
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)
我已经像这样添加了我的 LIBS 和我的 INCLUDEPATH:
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
举个例子,当我尝试在我的主函数中调用如下代码时:
#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;
我遇到了很多这样的错误:
/tmp/cirrus-ci-build/PcapPlusPlus/Common++/header/Logger.h:173: error : undefined reference to `pcpp::LoggerPP::LoggerPP()'
【问题讨论】:
【参考方案1】:想法是将 .mk 转换为 qmake 理解的适当代码,例如在我的情况下 .mk 是:
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)
因此使用前面的信息构建以下.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
【讨论】:
好的,我想,我只能说谢谢你完美的工作!主题已关闭!以上是关于配置 Qt 项目(.pro 文件)以使用 PcapPlusPlus的主要内容,如果未能解决你的问题,请参考以下文章