简体   繁体   English

静态库链接失败

[英]Static library linking failure

My OS is Windows 7 Ultimate, x64. 我的操作系统是Windows 7 Ultimate,x64。 I compiled the library ( https://github.com/mfontanini/libtins ) with cmake, Visual Studio 12.0. 我使用cmake Visual Studio 12.0编译了库( https://github.com/mfontanini/libtins )。 The output was tins.lib. 输出为tins.lib。 Dependencies: Winpcap, IHPlpapi.lib and WS2_32.lib. 依赖项:Winpcap,IHPlpapi.lib和WS2_32.lib。 Winpcap seemed to work alone, I got all includes at C:\\libtins\\Include, and all .lib files in C:\\libtins except winpcap. Winpcap似乎可以单独工作,我在C:\\ libtins \\ Include中包含了所有包含文件,并且除了winpcap以外,C:\\ libtins中的所有.lib文件都包含在内。 The main.cpp source was taken from a library-example. main.cpp源来自库示例。 I am unable to understand what's the problem, it seems it fails to read some functions. 我无法理解问题所在,似乎无法阅读某些功能。 Compiler for the following source is Qt Creator. 以下源代码的编译器是Qt Creator。

Why does the library fail to link with Qt Creator? 为什么库无法与Qt Creator链接? Moreover, what's the solution to get this error fixed? 此外,解决此错误的解决方案是什么?

Qt project file is this: Qt项目文件是这样的:

QT       += core
QT       -= gui

TARGET = libtins_test
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += C:/WpdPack/Include
INCLUDEPATH += C:/libtins/Include
LIBS += -LC:/WpdPack/Lib/x64 -lwpcap -lpacket
LIBS += -LC:/libtins -lws2_32 -liphlpapi -ltins

main.cpp is this: main.cpp是这样的:

#include <tins/tins.h>
#include <map>
#include <iostream>
#include <functional>
#include <QCoreApplication>

using namespace Tins;

class arp_monitor {
public:
    void run(Sniffer &sniffer);
private:
    bool callback(const PDU &pdu);

    std::map<IPv4Address, HWAddress<6>> addresses;
};

void arp_monitor::run(Sniffer &sniffer)
{
    sniffer.sniff_loop(
        std::bind(
            &arp_monitor::callback,
            this,
            std::placeholders::_1
        )
    );
}

bool arp_monitor::callback(const PDU &pdu)
{
    // Retrieve the ARP layer
    const ARP &arp = pdu.rfind_pdu<ARP>();
    // Is it an ARP reply?
    if(arp.opcode() == ARP::REPLY) {
        // Let's check if there's already an entry for this address
        auto iter = addresses.find(arp.sender_ip_addr());
        if(iter == addresses.end()) {
            // We haven't seen this address. Save it.
            addresses.insert({ arp.sender_ip_addr(), arp.sender_hw_addr()});
            std::cout << "[INFO] " << arp.sender_ip_addr() << " is at "
                      << arp.sender_hw_addr() << std::endl;
        }
        else {
            // We've seen this address. If it's not the same HW address, inform it
            if(arp.sender_hw_addr() != iter->second) {
                std::cout << "[WARNING] " << arp.sender_ip_addr() << " is at "
                          << iter->second << " but also at " << arp.sender_hw_addr()
                          << std::endl;
            }
        }
    }
    return true;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
        /*
        if(argc != 2) {
            std::cout << "Usage: " << *argv << " <interface>\n";
            return 1;
        }*/
    arp_monitor monitor;
    // Sniffer configuration
    SnifferConfiguration config;
    config.set_promisc_mode(true);
    config.set_filter("arp");

    // Sniff on the provided interface in promiscuous mode
    Sniffer sniffer("eth0", config);

    // Only capture arp packets
    monitor.run(sniffer);
    return a.exec();
}

Compile output (Qt Creator): 编译输出(Qt Creator):

03:13:11: Running steps for project libtins_test...
03:13:11: Configuration unchanged, skipping qmake step.
03:13:11: Starting: "C:\Qt\Tools\QtCreator\bin\jom.exe" 
    C:\Qt\Tools\QtCreator\bin\jom.exe -f Makefile.Debug
    echo 1 /* CREATEPROCESS_MANIFEST_RESOURCE_ID */ 24 /* RT_MANIFEST */          "debug\\libtins_test.exe.embed.manifest">debug\libtins_test.exe_manifest.rc
if not exist debug\libtins_test.exe if exist debug\libtins_test.exe.embed.manifest del debug\libtins_test.exe.embed.manifest
if exist debug\libtins_test.exe.embed.manifest copy /Y debug\libtins_test.exe.embed.manifest debug\libtins_test.exe_manifest.bak
link /NOLOGO /DYNAMICBASE /NXCOMPAT /DEBUG /SUBSYSTEM:CONSOLE "/MANIFESTDEPENDENCY:type='win32'     name='Microsoft.Windows.Common-Controls' version='6.0.0.0' publicKeyToken='6595b64144ccf1df'     language='*' processorArchitecture='*'" /MANIFEST /MANIFESTFILE:debug\libtins_test.exe.embed.manifest     /OUT:debug\libtins_test.exe @C:\Users\burny1\AppData\Local\Temp\libtins_test.exe.3536.47.jom
main.obj : 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::ARP::sender_ip_addr(void)const " (?    sender_ip_addr@ARP@Tins@@QEBA?AVIPv4Address@2@XZ)
main.obj : 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 "private: bool __cdecl     arp_monitor::callback(class Tins::PDU const &)" (?callback@arp_monitor@@AEAA_NAEBVPDU@Tins@@@Z)
main.obj : 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)
main.obj : 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)
main.obj : 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<class std::_Bind<1,bool,struct     std::_Pmf_wrap<bool (__cdecl arp_monitor::*)(class Tins::PDU const &),bool,class arp_monitor,class     Tins::PDU const &>,class arp_monitor * const,class std::_Ph<1> &> >(class std::_Bind<1,bool,struct     std::_Pmf_wrap<bool (__cdecl arp_monitor::*)(class Tins::PDU const &),bool,class arp_monitor,class     Tins::PDU const &>,class arp_monitor * const,class std::_Ph<1> &>,unsigned int)" (??$sniff_loop@V?    $_Bind@$00_NU?$_Pmf_wrap@P8arp_monitor@@EAA_NAEBVPDU@Tins@@@Z_NV1@AEBV23@@std@@QEAVarp_monitor@@AEAV?    $_Ph@$00@2@@std@@@BaseSniffer@Tins@@QEAAXV?$_Bind@$00_NU?    $_Pmf_wrap@P8arp_monitor@@EAA_NAEBVPDU@Tins@@@Z_NV1@AEBV23@@std@@QEAVarp_monitor@@AEAV?    $_Ph@$00@2@@std@@I@Z)
main.obj : 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<class std::_Bind<1,bool,struct std::_Pmf_wrap<bool (__cdecl arp_monitor::*)(class Tins::PDU const &),bool,class arp_monitor,class Tins::PDU const &>,class arp_monitor * const,class std::_Ph<1> &> >(class std::_Bind<1,bool,struct std::_Pmf_wrap<bool (__cdecl arp_monitor::*)(class Tins::PDU const &),bool,class arp_monitor,class Tins::PDU const &>,class arp_monitor * const,class std::_Ph<1> &>,unsigned int)" (??$sniff_loop@V?    $_Bind@$00_NU?$_Pmf_wrap@P8arp_monitor@@EAA_NAEBVPDU@Tins@@@Z_NV1@AEBV23@@std@@QEAVarp_monitor@@AEAV?    $_Ph@$00@2@@std@@@BaseSniffer@Tins@@QEAAXV?$_Bind@$00_NU?    $_Pmf_wrap@P8arp_monitor@@EAA_NAEBVPDU@Tins@@@Z_NV1@AEBV23@@std@@QEAVarp_monitor@@AEAV?    $_Ph@$00@2@@std@@I@Z)
main.obj : 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 &,class Tins::SnifferConfiguration const &)" (??0Sniffer@Tins@@QEAA@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AEBVSnifferConfiguration@1@@Z) referenced in function main
main.obj : error LNK2019: unresolved external symbol "public: __cdecl     Tins::SnifferConfiguration::SnifferConfiguration(void)" (??0SnifferConfiguration@Tins@@QEAA@XZ)     referenced in function main
main.obj : error LNK2019: unresolved external symbol "public: void __cdecl     Tins::SnifferConfiguration::set_promisc_mode(bool)" (?    set_promisc_mode@SnifferConfiguration@Tins@@QEAAX_N@Z) referenced in function main
main.obj : error LNK2019: unresolved external symbol "public: void __cdecl     Tins::SnifferConfiguration::set_filter(class std::basic_string<char,struct     std::char_traits<char>,class std::allocator<char> > const &)" (?    set_filter@SnifferConfiguration@Tins@@QEAAXAEBV?$basic_string@DU?$char_traits@D@std@@V?    $allocator@D@2@@std@@@Z) referenced in function main
debug\libtins_test.exe : fatal error LNK1120: 10 unresolved externals
jom: C:\Qt\Tools\QtCreator\bin\build-libtins_test-Desktop_Qt_5_3_MSVC2013_64bit-Debug\Makefile.Debug     [debug\libtins_test.exe] Error 1120
jom: C:\Qt\Tools\QtCreator\bin\build-libtins_test-Desktop_Qt_5_3_MSVC2013_64bit-Debug\Makefile         [debug] Error 2
03:13:12: The process "C:\Qt\Tools\QtCreator\bin\jom.exe" exited with code 2.
Error while building/deploying project libtins_test (kit: Desktop Qt 5.3 MSVC2013 64bit)
When executing step "Make"
03:13:12: Elapsed time: 00:01.

Including the libraries using -L and -l notations has never worked for me using the MSVC compiler. 使用MSVC编译器对我来说,使用-L和-l表示法包含库从来没有用过。 I have no idea why though. 我不知道为什么。 Try including the libraries with full paths including the extensions. 尝试包括具有完整路径(包括扩展名)的库。 For ex. 对于前。

LIBS += C:/WpdPack/Lib/x64/wpcap.lib \
        C:/WpdPack/Lib/x64/packet.lib \
        C:/libtins/ws2_32.lib \
        C:/libtins/iphlpapi.lib \
        C:/libtins/tins.lib

Even the order in which you include these libraries will matter in the case of MSVC. 对于MSVC,甚至包括这些库的顺序也很重要。 If one library is dependent on the other library, the top level library has to be on the top. 如果一个库依赖于另一个库,则顶层库必须位于顶层。 ie if A.lib is dependent on B.lib, you have to add A first followed by B. 例如,如果A.lib依赖于B.lib,则必须先添加A,然后再添加B。

This is because x64 libs set is incomplete in WpdPack. 这是因为WpdPack中x64库的设置不完整。 You can try 32bit or build x64 bit ownself. 您可以尝试32位或自己构建x64位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM