I am building a Qt application (with CMake) that will capture and analyze some network packets. Since I am using C++ all over the project, it would be convenient for me to use Pcap++ in my application rather than using lower lever C APIs provided by libs such as libpcap or winpcap.
However I find it hard to use pre-built Pcap++ libraries in my CMakeLists.txt given that no much info was provided on how such an integration would look like.
The application is supposed to be cross-platform and I started with integrating Pcap++ on windows first. So I have downloaded pre-built pcapplusplus-22.05-windows-mingw32-gcc-9.2.0. And then tried to follow instructions from Readme and apply them to my CMake file.
I post here the relevant parts of CMakeLists to show how I have attempted to do it:
set(THIRD_PARTY_LIBS "")
set(THIRD_PARTY_INCLUDES "")
# Add Pcap++ headers
list(APPEND THIRD_PARTY_INCLUDES ${CMAKE_CURRENT_LIST_DIR}/pcapplusplus-22.05-windows-mingw32-gcc-9.2.0/header)
# Pcap++ libs and dependencies according to their README
list(APPEND THIRD_PARTY_LIBS pthread ws2_32 iphlpapi)
list(APPEND THIRD_PARTY_LIBS ${CMAKE_CURRENT_LIST_DIR}/pcapplusplus-22.05-windows-mingw32-gcc-9.2.0/Common++.lib)
list(APPEND THIRD_PARTY_LIBS ${CMAKE_CURRENT_LIST_DIR}/pcapplusplus-22.05-windows-mingw32-gcc-9.2.0/Packet++.lib)
list(APPEND THIRD_PARTY_LIBS ${CMAKE_CURRENT_LIST_DIR}/pcapplusplus-22.05-windows-mingw32-gcc-9.2.0/Pcap++.lib)
# Add WinPcap libs
list(APPEND THIRD_PARTY_LIBS ${CMAKE_CURRENT_LIST_DIR}/WpdPack/Lib/Packet.lib ${CMAKE_CURRENT_LIST_DIR}/WpdPack/Lib/wpcap.lib)
# Add includes to project
include_directories(${THIRD_PARTY_INCLUDES})
# Add libs to project
target_link_libraries(${PROJECT_NAME} PRIVATE ${THIRD_PARTY_LIBS} Qt${QT_VERSION_MAJOR}::Core)
The relative locations are fine. Yet, when application is linking, the following error is occurring:
cmd.exe /C "cd . && C:\Qt\Tools\mingw1120_64\bin\c++.exe -g -mwindows CMakeFiles/MyProjectName.dir/MyProjectName_autogen/mocs_compilation.cpp.obj CMakeFiles/MyProjectName.dir/src/core/AppLogger.cpp.obj CMakeFiles/MyProjectName.dir/src/main.cpp.obj CMakeFiles/MyProjectName.dir/src/utils/utils.cpp.obj CMakeFiles/MyProjectName.dir/ui/mainwindow.cpp.obj CMakeFiles/MyProjectName.dir/res/win.rc.obj CMakeFiles/MyProjectName.dir/MyProjectName_autogen/PNK5WDWK6L/qrc_resources.cpp.obj -o MyProjectName.exe -Wl,--out-implib,libMyProjectName.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -L<proj_loc>/src/core -L<proj_loc>/src/utils -L<proj_loc>/ui thirdparty/spdlog/libspdlogd.a ../../thirdparty/WpdPack/Lib/Packet.lib ../../thirdparty/WpdPack/Lib/wpcap.lib -lpthread -lws2_32 -liphlpapi ../../thirdparty/pcapplusplus-22.05-windows-mingw32-gcc-9.2.0/Common++.lib ../../thirdparty/pcapplusplus-22.05-windows-mingw32-gcc-9.2.0/Packet++.lib ../../thirdparty/pcapplusplus-22.05-windows-mingw32-gcc-9.2.0/Pcap++.lib C:/Qt/6.3.1/mingw_64/lib/libQt6Network.a C:/Qt/6.3.1/mingw_64/lib/libQt6Widgets.a -lws2_32 C:/Qt/6.3.1/mingw_64/lib/libQt6Gui.a C:/Qt/6.3.1/mingw_64/lib/libQt6Core.a -lmpr -luserenv -lmingw32 C:/Qt/6.3.1/mingw_64/lib/libQt6EntryPoint.a -lshell32 -ld3d11 -ldxgi -ldxguid -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:/Qt/Tools/mingw1120_64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/MyProjectName.dir/ui/mainwindow.cpp.obj:<proj_loc>/thirdparty/pcapplusplus-22.05-windows-mingw32-gcc-9.2.0/header/pcappp/PcapLiveDeviceList.h:47: undefined reference to `pcpp::PcapLiveDeviceList::~PcapLiveDeviceList()'
C:/Qt/Tools/mingw1120_64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/MyProjectName.dir/ui/mainwindow.cpp.obj: in function `MainWindow::on_pushButton_StartRecording_clicked()':
<proj_loc>/ui/mainwindow.cpp:36: undefined reference to `pcpp::PcapLiveDeviceList::getPcapLiveDeviceByIp(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const'
C:/Qt/Tools/mingw1120_64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/MyProjectName.dir/ui/mainwindow.cpp.obj: in function `pcpp::PcapLiveDeviceList::getInstance()':
<proj_loc>/thirdparty/pcapplusplus-22.05-windows-mingw32-gcc-9.2.0/header/pcappp/PcapLiveDeviceList.h:47: undefined reference to `pcpp::PcapLiveDeviceList::PcapLiveDeviceList()'
collect2.exe: error: ld returned 1 exit status
In my application I am executing this simple function to get a network adapter by it's IP address:
this->dev = = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp("1.2.3.4")
Seeing "undefined reference to" makes me think that my libs were not properly included. How do I properly do it?
Later edit: It seems my application is 64bit but the libraries are 32bit compiled. This is probably my issue. However I am wondering why no warnings are visible to indicate this