静态库链接失败

时间:2014-10-18 00:21:27

标签: c++ windows qt qt-creator

我的操作系统是Windows 7 Ultimate,x64。我用cmake,Visual Studio 12.0编译了库(https://github.com/mfontanini/libtins)。输出是tins.lib。依赖关系:Winpcap,IHPlpapi.lib和WS2_32.lib。 Winpcap似乎单独工作,我得到C:\ libtins \ Include的所有包含,以及C:\ libtins中除winpcap之外的所有.lib文件。 main.cpp源代码来自库示例。我无法理解问题是什么,似乎无法读取某些功能。以下源代码的编译器是Qt Creator。

为什么图书馆无法与Qt Creator链接?此外,解决此错误的解决方案是什么?

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是这样的:

#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();
}

编译输出(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.

2 个答案:

答案 0 :(得分:0)

使用-L和-l表示法包含库对我来说从未使用过MSVC编译器。我不知道为什么。尝试将库包含在包含扩展名的完整路径中。对于前。

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

在MSVC的情况下,即使包含这些库的顺序也很重要。如果一个库依赖于另一个库,则顶级库必须位于顶部。即如果A.lib依赖于B.lib,则必须首先添加A,然后添加B.

答案 1 :(得分:0)

这是因为在WpdPack中设置的x64库不完整。您可以尝试32位或自己构建x64位。