seladb / PcapPlusPlus

PcapPlusPlus is a multiplatform C++ library for capturing, parsing and crafting of network packets. It is designed to be efficient, powerful and easy to use. It provides C++ wrappers for the most popular packet processing engines such as libpcap, Npcap, WinPcap, DPDK, AF_XDP and PF_RING.
https://pcapplusplus.github.io/
The Unlicense
2.63k stars 641 forks source link

Error linking pcapplusplus using vcpkg and CMake. #1349

Closed AbderrahmaneBr closed 3 months ago

AbderrahmaneBr commented 3 months ago

I'm using Cmake and vcpkg to link Pcap++ to my environment.

here's the Cmake CMakeLists.txt file:

cmake_minimum_required(VERSION 3.10)
project(NetSentinel)

# Or use the header-only version
find_package(fmt CONFIG REQUIRED)

# Add your executable
add_executable(${PROJECT_NAME} src/main.cpp)

# Link PcapPlusPlus to your executable
target_link_libraries(${PROJECT_NAME} PRIVATE fmt::fmt)

here's src/main.cpp

#include <iostream>
#include <pcapplusplus/IPv4Layer.h>
#include <pcapplusplus/Packet.h>
#include <pcapplusplus/PcapFileDevice.h>

int main(int argc, char* argv[])
{
    // open a pcap file for reading
    pcpp::PcapFileReaderDevice reader("1_packet.pcap");
    if (!reader.open())
    {
        std::cerr << "Error opening the pcap file" << std::endl;
        return 1;
    }

    // read the first (and only) packet from the file
    pcpp::RawPacket rawPacket;
    if (!reader.getNextPacket(rawPacket))
    {
        std::cerr << "Couldn't read the first packet in the file" << std::endl;
        return 1;
    }

    // parse the raw packet into a parsed packet
    pcpp::Packet parsedPacket(&rawPacket);

    // verify the packet is IPv4
    if (parsedPacket.isPacketOfType(pcpp::IPv4))
    {
        // extract source and dest IPs
        pcpp::IPv4Address srcIP = parsedPacket.getLayerOfType<pcpp::IPv4Layer>()->getSrcIPv4Address();
        pcpp::IPv4Address destIP = parsedPacket.getLayerOfType<pcpp::IPv4Layer>()->getDstIPv4Address();

        // print source and dest IPs
        std::cout
            << "Source IP is '" << srcIP << "'; "
            << "Dest IP is '" << destIP << "'"
            << std::endl;
    }

    // close the file
    reader.close();

    return 0;
}

here's the Error i get when building the project:

C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NetSentinel.dir/objects.a(main.cpp.obj):C:/Users/pc/Desktop/Projects/NetSentinel/src/main.cpp:10: undefined reference to `pcpp::PcapFileReaderDevice::open()'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NetSentinel.dir/objects.a(main.cpp.obj): in function `main':
C:/Users/pc/Desktop/Projects/NetSentinel/src/main.cpp:17: undefined reference to `pcpp::RawPacket::RawPacket()'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/pc/Desktop/Projects/NetSentinel/src/main.cpp:18: undefined reference to `pcpp::PcapFileReaderDevice::getNextPacket(pcpp::RawPacket&)'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/pc/Desktop/Projects/NetSentinel/src/main.cpp:25: undefined reference to `pcpp::Packet::Packet(pcpp::RawPacket*, bool, unsigned long long, pcpp::OsiModelLayer)'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/pc/Desktop/Projects/NetSentinel/src/main.cpp:42: undefined reference to `pcpp::IFileDevice::close()'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/pc/Desktop/Projects/NetSentinel/src/main.cpp:17: undefined reference to `pcpp::RawPacket::~RawPacket()'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/pc/Desktop/Projects/NetSentinel/src/main.cpp:17: undefined reference to `pcpp::RawPacket::~RawPacket()'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NetSentinel.dir/objects.a(main.cpp.obj): in function `operator<<(std::ostream&, pcpp::IPv4Address const&)':
C:/vcpkg/installed/x64-windows/include/pcapplusplus/IpAddress.h:1010: undefined reference to `pcpp::IPv4Address::toString[abi:cxx11]() const'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NetSentinel.dir/objects.a(main.cpp.obj): in function `pcpp::Packet::~Packet()':
C:/vcpkg/installed/x64-windows/include/pcapplusplus/Packet.h:101: undefined reference to `pcpp::Packet::destructPacketData()'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NetSentinel.dir/objects.a(main.cpp.obj): in function `pcpp::IFileReaderDevice::~IFileReaderDevice()':
C:/vcpkg/installed/x64-windows/include/pcapplusplus/PcapFileDevice.h:72: undefined reference to `pcpp::IFileDevice::~IFileDevice()'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NetSentinel.dir/objects.a(main.cpp.obj): in function `pcpp::PcapFileReaderDevice::PcapFileReaderDevice(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
C:/vcpkg/installed/x64-windows/include/pcapplusplus/PcapFileDevice.h:119: undefined reference to `pcpp::IFileReaderDevice::IFileReaderDevice(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NetSentinel.dir/objects.a(main.cpp.obj):main.cpp:(.rdata$_ZTVN4pcpp17IFileReaderDeviceE[_ZTVN4pcpp17IFileReaderDeviceE]+0x28): undefined reference to `pcpp::IFileDevice::close()'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NetSentinel.dir/objects.a(main.cpp.obj):main.cpp:(.rdata$_ZTVN4pcpp17IFileReaderDeviceE[_ZTVN4pcpp17IFileReaderDeviceE]+0x38): undefined reference to `pcpp::IPcapDevice::setFilter(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NetSentinel.dir/objects.a(main.cpp.obj):main.cpp:(.rdata$_ZTVN4pcpp17IFileReaderDeviceE[_ZTVN4pcpp17IFileReaderDeviceE]+0x40): undefined reference to `pcpp::IPcapDevice::clearFilter()'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NetSentinel.dir/objects.a(main.cpp.obj):main.cpp:(.rdata$_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> >)'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NetSentinel.dir/objects.a(main.cpp.obj):main.cpp:(.rdata$_ZTVN4pcpp17IFileReaderDeviceE[_ZTVN4pcpp17IFileReaderDeviceE]+0x80): undefined reference to `non-virtual thunk to pcpp::IPcapDevice::clearFilter()'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/NetSentinel.dir/objects.a(main.cpp.obj):main.cpp:(.rdata$.refptr._ZTVN4pcpp20PcapFileReaderDeviceE[.refptr._ZTVN4pcpp20PcapFileReaderDeviceE]+0x0): undefined reference to `vtable for pcpp::PcapFileReaderDevice'
collect2.exe: error: ld returned 1 exit status
make[2]: *** [NetSentinel.exe] Error 1
make[1]: *** [CMakeFiles/NetSentinel.dir/all] Error 2
make: *** [all] Error 2

I've been trying to fix this problem for the past week but couldn't, can somebody help?

tigercosmos commented 3 months ago

an example:

# Find PcapPlusPlus library
find_package(PcapPlusPlus REQUIRED)

# Add your project source files and link against PcapPlusPlus library
add_executable(hello_world hello_world.cpp)
target_link_libraries(hello_world PcapPlusPlus::Packet++ PcapPlusPlus::Pcap++ PcapPlusPlus::Common++)
AbderrahmaneBr commented 3 months ago

Thanks it helped!