通过CMake将pcapplusplus包含在项目中



我在Ubuntu上安装了pcapplusplus(从这里下载的软件包:https://github.com/seladb/PcapPlusPlus/releases/tag/v21.11)。档案中的例子编译并工作,一切都很好!但是当我尝试使用CMake将库包含在我的项目中时,什么都不起作用。

我在CMakeLists.txt文件中写了一行:

include_directories("/usr/local/include/pcapplusplus")

之后,头文件将连接到项目。然而,该项目没有编译,根据我使用的函数会出现各种错误。链接器很可能看不到文件:libCommon++.a、libPacket++.a和libPcap++.a。我试着这样连接它们:

target_link_libraries(${PROJECT_NAME} libCommon++.a libPacket++.a libPcap++.a)

但这无济于事。尝试过这个:

find_package(pcapplusplus REQUIRED)
include_directories(${PCAPPLUSPLUS_INCLUDE_DIRS})

这也无济于事。

事实上,其他人已经遇到过这样的问题,例如,netleap tom在StackOverflow上写过这件事。cmake链接静态库-你必须告诉cmake在哪里查找吗?然而,那里没有人向他提出解决方案。我希望有人能告诉我该怎么做。

udp。

Hello World,例如:

#include <IPv4Layer.h>
#include <Packet.h>
#include <PcapFileDevice.h>
int main(int argc, char* argv[])
{
pcpp::PcapFileReaderDevice reader("1_packet.pcap");
if (!reader.open())
{
printf("Error opening the pcap filen");
return 1;
}
pcpp::RawPacket rawPacket;
if (!reader.getNextPacket(rawPacket))
{
printf("Couldn't read the first packet in the filen");
return 1;
}
if (parsedPacket.isPacketOfType(pcpp::IPv4))
{
pcpp::IPv4Address srcIP = parsedPacket.getLayerOfType<pcpp::IPv4Layer>()->getSrcIpAddress();
pcpp::IPv4Address destIP = parsedPacket.getLayerOfType<pcpp::IPv4Layer>()->getDstIpAddress();
printf("Source IP is '%s'; Dest IP is '%s'n", srcIP.toString().c_str(), destIP.toString().c_str());
}
reader.close();
return 0;
}

如果我只将此添加到CMake:

include_directories("/usr/local/include/pcapplusplus")

我有以下错误:

/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o: in function `pcpp::Packet::~Packet()':
main.cpp:(.text._ZN4pcpp6PacketD2Ev[_ZN4pcpp6PacketD5Ev]+0x17): undefined reference to `pcpp::Packet::destructPacketData()'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o: in function `pcpp::Packet::~Packet()':
main.cpp:(.text._ZN4pcpp6PacketD0Ev[_ZN4pcpp6PacketD5Ev]+0x17): undefined reference to `pcpp::Packet::destructPacketData()'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o: in function `pcpp::IPv4Layer* pcpp::Packet::getLayerOfType<pcpp::IPv4Layer>(bool) const':
main.cpp:(.text._ZNK4pcpp6Packet14getLayerOfTypeINS_9IPv4LayerEEEPT_b[_ZNK4pcpp6Packet14getLayerOfTypeINS_9IPv4LayerEEEPT_b]+0x1b): undefined reference to `typeinfo for pcpp::IPv4Layer'
/usr/bin/ld: main.cpp:(.text._ZNK4pcpp6Packet14getLayerOfTypeINS_9IPv4LayerEEEPT_b[_ZNK4pcpp6Packet14getLayerOfTypeINS_9IPv4LayerEEEPT_b]+0x22): undefined reference to `typeinfo for pcpp::Layer'
/usr/bin/ld: main.cpp:(.text._ZNK4pcpp6Packet14getLayerOfTypeINS_9IPv4LayerEEEPT_b[_ZNK4pcpp6Packet14getLayerOfTypeINS_9IPv4LayerEEEPT_b]+0x4e): undefined reference to `typeinfo for pcpp::IPv4Layer'
/usr/bin/ld: main.cpp:(.text._ZNK4pcpp6Packet14getLayerOfTypeINS_9IPv4LayerEEEPT_b[_ZNK4pcpp6Packet14getLayerOfTypeINS_9IPv4LayerEEEPT_b]+0x55): undefined reference to `typeinfo for pcpp::Layer'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o: in function `main.cold':
main.cpp:(.text.unlikely+0x58): undefined reference to `pcpp::Packet::destructPacketData()'
/usr/bin/ld: main.cpp:(.text.unlikely+0x63): undefined reference to `pcpp::RawPacket::~RawPacket()'
/usr/bin/ld: main.cpp:(.text.unlikely+0x8a): undefined reference to `pcpp::IFileDevice::~IFileDevice()'

更多信息:图片。

如果我将此添加到CMake:

target_link_libraries(${PROJECT_NAME} libCommon++.a libPacket++.a libPcap++.a)

我有以下错误(前五个):

/usr/bin/ld: /usr/local/lib/libPacket++.a(EthLayer.o): in function `pcpp::EthLayer::toString[abi:cxx11]() const':
/tmp/cirrus-ci-build/PcapPlusPlus/Packet++/src/EthLayer.cpp:104: undefined reference to `pcpp::MacAddress::toString[abi:cxx11]() const'
/usr/bin/ld: /tmp/cirrus-ci-build/PcapPlusPlus/Packet++/src/EthLayer.cpp:104: undefined reference to `pcpp::MacAddress::toString[abi:cxx11]() const'
/usr/bin/ld: /usr/local/lib/libPacket++.a(EthDot3Layer.o): in function `pcpp::EthDot3Layer::toString[abi:cxx11]() const':
/tmp/cirrus-ci-build/PcapPlusPlus/Packet++/src/EthDot3Layer.cpp:36: undefined reference to `pcpp::MacAddress::toString[abi:cxx11]() const'
/usr/bin/ld: /tmp/cirrus-ci-build/PcapPlusPlus/Packet++/src/EthDot3Layer.cpp:36: undefined reference to `pcpp::MacAddress::toString[abi:cxx11]() const'
/usr/bin/ld: /usr/local/lib/libPacket++.a(DhcpLayer.o): in function `pcpp::DhcpLayer::getClientHardwareAddress() const':
/tmp/cirrus-ci-build/PcapPlusPlus/Packet++/src/DhcpLayer.cpp:83: undefined reference to `pcpp::MacAddress::Zero'
/usr/bin/ld: /usr/local/lib/libPacket++.a(PayloadLayer.o): in function `pcpp::PayloadLayer::PayloadLayer(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/tmp/cirrus-ci-build/PcapPlusPlus/Packet++/src/PayloadLayer.cpp:24: undefined reference to `pcpp::hexStringToByteArray(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned char*, unsigned long)'

更多信息:image2

undefined reference to `pcpp::IFileReaderDevice::IFileReaderDevice(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: main.cpp:(.text.startup+0x63): undefined reference to `vtable for pcpp::PcapFileReaderDevice'
/usr/bin/ld: main.cpp:(.text.startup+0xb5): undefined reference to `pcpp::IFileDevice::~IFileDevice()'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o:(.data.rel.ro._ZTIN4pcpp17IFileReaderDeviceE[_ZTIN4pcpp17IFileReaderDeviceE]+0x10): undefined reference to `typeinfo for pcpp::IFileDevice'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o:(.data.rel.ro._ZTVN4pcpp17IFileReaderDeviceE[_ZTVN4pcpp17IFileReaderDeviceE]+0x28): undefined reference to `pcpp::IFileDevice::close()'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o:(.data.rel.ro._ZTVN4pcpp17IFileReaderDeviceE[_ZTVN4pcpp17IFileReaderDeviceE]+0x38): undefined reference to `pcpp::IPcapDevice::setFilter(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o:(.data.rel.ro._ZTVN4pcpp17IFileReaderDeviceE[_ZTVN4pcpp17IFileReaderDeviceE]+0x40): undefined reference to `pcpp::IPcapDevice::clearFilter()'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o:(.data.rel.ro._ZTVN4pcpp17IFileReaderDeviceE[_ZTVN4pcpp17IFileReaderDeviceE]+0x78): undefined reference to `non-virtual thunk to pcpp::IPcapDevice::setFilter(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o:(.data.rel.ro._ZTVN4pcpp17IFileReaderDeviceE[_ZTVN4pcpp17IFileReaderDeviceE]+0x80): undefined reference to `non-virtual thunk to pcpp::IPcapDevice::clearFilter()'
collect2: error: ld returned 1 exit status

尝试这个

target_link_libraries(${PROJECT_NAME} Pcap++ Packet++ Common++ pcap pthread)

这是pcapplusplus示例mk文件/usr/local/etc/pcapplusplus.mk 中列出的顺序

解决方案:https://github.com/gleb-kun/pcappp_hw

不出所料,由于使用CMake的库连接不正确,导致出现错误。

需要文件FindPcapPlusPlus.cmake进行连接。将以下行添加到主文件CMakeLists.txt中:

find_package(PcapPlusPlus REQUIRED)
target_link_libraries(${PROJECT_NAME} ${PcapPlusPlus_LIBRARIES})

FindPcapPlusPlus.cmake文件内容:

if (PC_PcapPlusPlus_INCLUDEDIR AND PC_PcapPlusPlus_LIBDIR)
set(PcapPlusPlus_FIND_QUIETLY TRUE)
endif ()
find_package(PkgConfig REQUIRED)
pkg_check_modules(PC_PcapPlusPlus REQUIRED PcapPlusPlus)
set(PcapPlusPlus_VERSION ${PC_PcapPlusPlus_VERSION})
mark_as_advanced(PcapPlusPlus_INCLUDE_DIR PcapPlusPlus_LIBRARY)
foreach (LIB_NAME ${PC_PcapPlusPlus_LIBRARIES})
find_library(${LIB_NAME}_PATH ${LIB_NAME} HINTS ${PC_PcapPlusPlus_LIBDIR})
if (${LIB_NAME}_PATH)
list(APPEND PcapPlusPlus_LIBS ${${LIB_NAME}_PATH})
endif ()
endforeach ()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(PcapPlusPlus
REQUIRED_VARS PC_PcapPlusPlus_INCLUDEDIR PC_PcapPlusPlus_LIBDIR
VERSION_VAR PcapPlusPlus_VERSION
)
if (PcapPlusPlus_FOUND)
set(PcapPlusPlus_INCLUDE_DIRS ${PC_PcapPlusPlus_INCLUDEDIR})
set(PcapPlusPlus_LIBRARIES ${PcapPlusPlus_LIBS})
endif ()
if (PcapPlusPlus_FOUND AND NOT TARGET PcapPlusPlus::PcapPlusPlus)
add_library(PcapPlusPlus::PcapPlusPlus INTERFACE IMPORTED)
set_target_properties(PcapPlusPlus::PcapPlusPlus PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${PcapPlusPlus_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES "${PcapPlusPlus_LIBRARIES}"
INTERFACE_COMPILE_FLAGS "${PC_PcapPlusPlus_CFLAGS}"
)
endif ()

最新更新