mfontanini / libtins

High-level, multiplatform C++ network packet sniffing and crafting library.
http://libtins.github.io/
BSD 2-Clause "Simplified" License
1.89k stars 373 forks source link

'unresolved external symbol' in Visual Studio 2022 #514

Open etkaar opened 10 months ago

etkaar commented 10 months ago

The example code unfortunately can't be compiled with Visual Studio 2022:

#define TINS_STATIC

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

using namespace Tins;
using namespace std;

bool callback(const PDU& pdu) {
    // Find the IP layer
    const IP& ip = pdu.rfind_pdu<IP>();
    // Find the TCP layer
    const TCP& tcp = pdu.rfind_pdu<TCP>();
    cout << ip.src_addr() << ':' << tcp.sport() << " -> "
        << ip.dst_addr() << ':' << tcp.dport() << endl;
    return true;
}

int main() {
    Sniffer("eth0").sniff_loop(callback);
}

Errors

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol "public: __cdecl Tins::IPv4Address::IPv4Address(unsigned int)" (??0IPv4Address@Tins@@QEAA@I@Z) referenced in function "public: class Tins::IPv4Address __cdecl Tins::IP::dst_addr(void)const " (?dst_addr@IP@Tins@@QEBA?AVIPv4Address@2@XZ)  FooBar  D:\Dev\Visual Studio\repos\FooBar\FooBar.obj    1   
Error   LNK2019 unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl Tins::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Tins::IPv4Address const &)" (??6Tins@@YAAEAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AEAV12@AEBVIPv4Address@0@@Z) referenced in function "bool __cdecl callback(class Tins::PDU const &)" (?callback@@YA_NAEBVPDU@Tins@@@Z)  FooBar  D:\Dev\Visual Studio\repos\FooBar\FooBar.obj    1   
Error   LNK2019 unresolved external symbol "public: virtual __cdecl Tins::BaseSniffer::~BaseSniffer(void)" (??1BaseSniffer@Tins@@UEAA@XZ) referenced in function "public: virtual __cdecl Tins::Sniffer::~Sniffer(void)" (??1Sniffer@Tins@@UEAA@XZ) FooBar  D:\Dev\Visual Studio\repos\FooBar\FooBar.obj    1   
Error   LNK2019 unresolved external symbol "public: class Tins::PacketWrapper<class Tins::PDU *,class Tins::Timestamp> __cdecl Tins::BaseSniffer::next_packet(void)" (?next_packet@BaseSniffer@Tins@@QEAA?AV?$PacketWrapper@PEAVPDU@Tins@@VTimestamp@2@@2@XZ) referenced in function "private: void __cdecl Tins::SnifferIterator::advance(void)" (?advance@SnifferIterator@Tins@@AEAAXXZ)  FooBar  D:\Dev\Visual Studio\repos\FooBar\FooBar.obj    1   
Error   LNK2019 unresolved external symbol "public: class Tins::SnifferIterator __cdecl Tins::BaseSniffer::begin(void)" (?begin@BaseSniffer@Tins@@QEAA?AVSnifferIterator@2@XZ) referenced in function "public: void __cdecl Tins::BaseSniffer::sniff_loop<bool (__cdecl*)(class Tins::PDU const &)>(bool (__cdecl*)(class Tins::PDU const &),unsigned int)" (??$sniff_loop@P6A_NAEBVPDU@Tins@@@Z@BaseSniffer@Tins@@QEAAXP6A_NAEBVPDU@1@@ZI@Z)   FooBar  D:\Dev\Visual Studio\repos\FooBar\FooBar.obj    1   
Error   LNK2019 unresolved external symbol "public: class Tins::SnifferIterator __cdecl Tins::BaseSniffer::end(void)" (?end@BaseSniffer@Tins@@QEAA?AVSnifferIterator@2@XZ) referenced in function "public: void __cdecl Tins::BaseSniffer::sniff_loop<bool (__cdecl*)(class Tins::PDU const &)>(bool (__cdecl*)(class Tins::PDU const &),unsigned int)" (??$sniff_loop@P6A_NAEBVPDU@Tins@@@Z@BaseSniffer@Tins@@QEAAXP6A_NAEBVPDU@1@@ZI@Z)   FooBar  D:\Dev\Visual Studio\repos\FooBar\FooBar.obj    1   
Error   LNK2019 unresolved external symbol "public: __cdecl Tins::Sniffer::Sniffer(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0Sniffer@Tins@@QEAA@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function main    FooBar  D:\Dev\Visual Studio\repos\FooBar\FooBar.obj    1   
Error   LNK1120 7 unresolved externals  FooBar  D:\Dev\Visual Studio\repos\FooBar\x64\Debug\FooBar.exe  1
WorstCodeWay commented 7 months ago

@etkaar Hi, do you solve it? I encounter problem very like this, but the unresolved symbol is Tins::Internals::increment(IPv4Address &)

etkaar commented 7 months ago

No, unfortunately not. I had to use Visual Studio 2015 instead: https://stackoverflow.com/a/44290942

You will later then be able to still use Visual Studio 2022 by changing the Platform Toolset:

image

WorstCodeWay commented 7 months ago

No, unfortunately not. I had to use Visual Studio 2015 instead: https://stackoverflow.com/a/44290942

You will later then be able to still use Visual Studio 2022 by changing the Platform Toolset:

image

It's more likely a different problem, because I have solved mine. I share my solution here, in case it helps some one.

I build my program with cmake and compile with Visual Studio 2019. It's important to add -DCMAKE_WINDOWS_EXPORT_ALL_SYSBOLS=true to cmake parameters when building, which means all symbols of libtins will be exported to tins.dll, and that solves my problem( I had inspected my tins.dll and cannot find definition of Tins::Internals::increment(IPv4Address &), this my situation)