0

I am trying to compile a project using Visual Studio Code on Windows that uses the libtins library but I am having trouble getting CMake to link the library. I followed the main guide located on libtin's github.io page (located here) and another guide from StackOverflow (located here) that uses Visual Studio, but I am using CMake so those instructions are not one-to-one for my situation. I am not familiar with CMake so my configurations are pulled from other places on StackOverflow and the CMake documentation. I have CMake downloaded and configured in Visual Studio Code by following this guide (it translates to Windows very easily).

My CMakeLists.txt file:

cmake_minimum_required(VERSION 3.0.0)

set(CMAKE_VERBOSE_MAKEFILE ON)

project(systemx_protocol)

include_directories(libtins/include npcap-sdk-1.13/Include)
link_directories(libtins/build/lib/Debug npcap-sdk-1.13/Lib/x64)

add_executable(systemx_protocol main.cpp)
target_link_libraries(systemx_protocol tins Packet Ws2_32 Iphlpapi wpcap)

My main.cpp file (from libtins's tutorial page with slight modifications):

#define TINS_STATIC

#include <iostream>
#include <tins/tins.h>

using namespace Tins;

int main() {
    EthernetII eth;
    IP *ip = new IP();
    TCP *tcp = new TCP();

    // tcp is ip's inner pdu
    ip->inner_pdu(tcp);

    // ip is eth's inner pdu
    eth.inner_pdu(ip);
    std::cout << "Hello, from systemx_protocol!\n";
}

I built libtins from source by cloning from the github repository and following the build instructions. I downloaded the Npcap SDK from here then ran the following commands to build libtins:

cd <full/path/to>/libtins
mkdir build
cd build
cmake ../ -DPCAP_ROOT_DIR="<full/path/to>/npcap-sdk-1.13" -DLIBTINS_ENABLE_WPA2=0 -DLIBTINS_BUILD_SHARED=0 -DLIBTINS_ENABLE_CXX11=1 -DLIBTINS_ENABLE_DOT11=0
cmake --build . --config Debug

The following is the full output from clicking "Build" in Visual Studio Code. It seems like CMake isn't properly linking libtins, but I'm not sure where the linking is failing.

[main] Building folder: <project>
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build "<full/path/to/project>/build" --config Debug --target all -j 18 --
[build] "C:\Program Files\CMake\bin\cmake.exe" -S"<full/path/to/project>" -B"<full/path/to/project>\build" --check-build-system CMakeFiles\Makefile.cmake 0
[build] "C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_start "<full/path/to/project>\build\CMakeFiles" "<full/path/to/project>\build\\CMakeFiles\progress.marks"
[build] C:/msys64/mingw64/bin/mingw32-make.exe  -f CMakeFiles\Makefile2 all
[build] mingw32-make[1]: Entering directory '<full/path/to/project>/build'
[build] C:/msys64/mingw64/bin/mingw32-make.exe  -f CMakeFiles\systemx_protocol.dir\build.make CMakeFiles/systemx_protocol.dir/depend
[build] mingw32-make[2]: Entering directory '<full/path/to/project>/build'
[build] "C:\Program Files\CMake\bin\cmake.exe" -E cmake_depends "MinGW Makefiles" "<full/path/to/project>" "<full/path/to/project>" "<full/path/to/project>\build" "<full/path/to/project>\build" "<full/path/to/project>\build\CMakeFiles\systemx_protocol.dir\DependInfo.cmake" --color=
[build] mingw32-make[2]: Leaving directory '<full/path/to/project>/build'
[build] C:/msys64/mingw64/bin/mingw32-make.exe  -f CMakeFiles\systemx_protocol.dir\build.make CMakeFiles/systemx_protocol.dir/build
[build] mingw32-make[2]: Entering directory '<full/path/to/project>/build'
[build] [ 50%] Linking CXX executable systemx_protocol.exe
[build] "C:\Program Files\CMake\bin\cmake.exe" -E cmake_link_script CMakeFiles\systemx_protocol.dir\link.txt --verbose=1
[build] "C:\Program Files\CMake\bin\cmake.exe" -E rm -f CMakeFiles\systemx_protocol.dir/objects.a
[build] C:\msys64\mingw64\bin\ar.exe qc CMakeFiles\systemx_protocol.dir/objects.a @CMakeFiles\systemx_protocol.dir\objects1.rsp
[build] C:\msys64\mingw64\bin\g++.exe -g -Wl,--whole-archive CMakeFiles\systemx_protocol.dir/objects.a -Wl,--no-whole-archive -o systemx_protocol.exe -Wl,--out-implib,libsystemx_protocol.dll.a -Wl,--major-image-version,0,--minor-image-version,0 @CMakeFiles\systemx_protocol.dir\linkLibs.rsp
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\systemx_protocol.dir/objects.a(main.cpp.obj):<full/path/to/project>/main.cpp:10: undefined reference to `Tins::EthernetII::EthernetII(Tins::HWAddress<6ull> const&, Tins::HWAddress<6ull> const&)'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\systemx_protocol.dir/objects.a(main.cpp.obj): in function `main':
[build] <full/path/to/project>/main.cpp:11: undefined reference to `Tins::IPv4Address::IPv4Address(char const*)'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: <full/path/to/project>/main.cpp:11: undefined reference to `Tins::IPv4Address::IPv4Address(char const*)'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: <full/path/to/project>/main.cpp:11: undefined reference to `Tins::IP::IP(Tins::IPv4Address, Tins::IPv4Address)'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: <full/path/to/project>/main.cpp:12: undefined reference to `Tins::TCP::TCP(unsigned short, unsigned short)'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: <full/path/to/project>/main.cpp:15: undefined reference to `Tins::PDU::inner_pdu(Tins::PDU*)'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: <full/path/to/project>/main.cpp:18: undefined reference to `Tins::PDU::inner_pdu(Tins::PDU*)'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\systemx_protocol.dir/objects.a(main.cpp.obj): in function `Tins::EthernetII::~EthernetII()':
[build] <full/path/to/project>/libtins/include/tins/ethernetII.h:46: undefined reference to `Tins::PDU::~PDU()'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\systemx_protocol.dir/objects.a(main.cpp.obj):main.cpp:(.rdata$.refptr._ZTVN4Tins10EthernetIIE[.refptr._ZTVN4Tins10EthernetIIE]+0x0): undefined reference to `vtable for Tins::EthernetII'
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make[2]: *** [CMakeFiles\systemx_protocol.dir\build.make:102: systemx_protocol.exe] Error 1
[build] mingw32-make[2]: Leaving directory '<full/path/to/project>/build'
[build] mingw32-make[1]: Leaving directory '<full/path/to/project>/build'
[build] mingw32-make[1]: *** [CMakeFiles\Makefile2:85: CMakeFiles/systemx_protocol.dir/all] Error 2
[build] mingw32-make: *** [Makefile:113: all] Error 2
[proc] The command: "C:\Program Files\CMake\bin\cmake.EXE" --build "<full/path/to/project>/build" --config Debug --target all -j 18 -- exited with code: 2
[driver] Build completed: 00:00:01.190
[build] Build finished with exit code 2

My file structure looks like this (irrelevant files & directories omitted):

- <project>
    - main.cpp
    - CMakeLists.txt
    - libtins
        - build
            - lib
                - Debug
                    - tins.lib
        - include
            - tins
    - npcap-sdk-1.13
        - Include
        - Lib
            - x64
                - Packet.lib
                - wpcap.lib
    - build

What is causing the linking to fail? Am I using the wrong options for the CMakeLists.txt file?

asqapro
  • 175
  • 3
  • 15
  • ``CMakeFiles\systemx_protocol.dir\linkLibs.rsp`` contains the following: ``-L/libtins/build/lib/Debug -L"/NPCAP-~1.13/Lib/x64" -ltins -lPacket -lWs2_32 -lIphlpapi -lwpcap -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32`` – asqapro Jun 12 '23 at 20:33
  • Looks like you have built libtins for **Visual Studio** (`cmake --build . --config Debug` implies using *multi-configuration* CMake generator), but your project is building with **MinGW**. Visual Studio and MinGW binaries are not compatible. For build libtins for MinGW specify additional parameter `-G"MinGW Makefiles"` when configure the project (`cmake ../ -DPCAP_ROOT_DIR=...`). – Tsyvarev Jun 13 '23 at 08:13

0 Answers0