【问题标题】:C++ Qt5 Net-snmp LNK ErrorC++ Qt5 Net-snmp LNK 错误
【发布时间】:2014-10-16 11:13:46
【问题描述】:

我已经从 http://softlayer-ams.dl.sourceforge.net/project/net-snmp/net-snmp%20binaries/5.7-binaries/net-snmp-5.7.0-1.x86.exe

并安装。

现在,我正在尝试按照本教程进行说明 (http://www.net-snmp.org/wiki/index.php/TUT:Simple_Application)

我试图在我的 C++ Qt5 应用程序中使用 VC++ 2010 编译器中的 net-snmp,但我收到以下错误

snmptest.obj:-1: Fehler: LNK2019: unresolved external symbol __imp__generate_Ku referenced in    function "public: void __thiscall SnmpTest::doSnmp(void)" (?doSnmp@SnmpTest@@QAEXXZ)

snmptest.obj:-1: Fehler: LNK2019: unresolved external symbol __imp__snmp_sess_init referenced in function "public: void __thiscall SnmpTest::doSnmp(void)" (?doSnmp@SnmpTest@@QAEXXZ)

snmptest.obj:-1: Fehler: LNK2019: unresolved external symbol __imp__init_snmp referenced in function "public: void __thiscall SnmpTest::doSnmp(void)" (?doSnmp@SnmpTest@@QAEXXZ)

C:\build\debug\a.exe:-1: Fehler: LNK1120: 4 unresolved externals

snmptest.obj:-1: Fehler: LNK2001: unresolved external symbol __imp__usmHMACMD5AuthProtocol    

我将此行添加到我的 *.pro 文件中:

win32:INCLUDEPATH += "C:\snmp_5.7.0\include"

SnmpTest.h

#ifndef SNMPTEST_H
#define SNMPTEST_H

#include <QDebug>
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/library/transform_oids.h>

class SnmpTest
{
public:
    SnmpTest();
    void doSnmp();
};

#endif // SNMPTEST_H

SnmpTest.cpp

SnmpTest::SnmpTest()
{
}

void SnmpTest::doSnmp()
{
    const char *our_v3_passphrase = "XXXXX";
    struct snmp_session session, *ss;
    struct snmp_pdu *pdu;
    struct snmp_pdu *response;

    oid anOID[MAX_OID_LEN];
    size_t anOID_len = MAX_OID_LEN;

    struct variable_list *vars;
    int status;

    /*
    * Initialize the SNMP library
    */
    init_snmp("snmpapp");

    snmp_sess_init(&session);
    session.peername = "xxxxx";
    session.version=SNMP_VERSION_3;

    /* set the SNMPv3 user name */
    session.securityName = _strdup("YYYY");
    session.securityNameLen = strlen(session.securityName);

    /* set the security level to authenticated, but not encrypted */
    session.securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;

    /* set the authentication method to MD5 */
    session.securityAuthProto = usmHMACMD5AuthProtocol;
    session.securityAuthProtoLen = sizeof(usmHMACMD5AuthProtocol)/sizeof(oid);
    session.securityAuthKeyLen = USM_AUTH_KU_LEN;

    /* set the authentication key to a MD5 hashed version of our
      passphrase (which must be at least 8
      characters long) */
    if(generate_Ku(
            session.securityAuthProto,
            session.securityAuthProtoLen,
            (u_char *) our_v3_passphrase, strlen(our_v3_passphrase),
            session.securityAuthKey,
            &session.securityAuthKeyLen) != SNMPERR_SUCCESS)
    {
        qDebug() << "Error generating Ku from authentication pass phrase.";
    }
}

感谢您的帮助!

【问题讨论】:

    标签: c++ qt visual-c++ snmp net-snmp


    【解决方案1】:

    仅指定包含路径是不够的。当然,您需要链接到库,否则链接器将无法解析所有符号。试试

    win32 {
        INCLUDEPATH += "C:\snmp_5.7.0\include"
        LIBS+= -L"C:\snmp_5.7.0\lib" -lnetsnmp
    }
    

    或类似的东西。如果库位于应用程序旁边的其他位置,您甚至可以指定库的绝对路径。检查该库的调用方式。

    【讨论】:

    • 感谢您的回答。在目录 C:\snmp_5.7.0\lib 中有 4 个文件,分别为 netsnmp.lib、netsnmpagent.lib、netsnmpmibs.lib 和 netsnmptrapd.lib。我需要在名字前加上“l”吗?我重建了项目,但错误仍然存​​在:(
    • @user2357505:嗯,那些似乎是静态库,但这也应该可以工作。我建议检查哪些库提供这些符号。如果您需要所有这些,您可以使用单独的-lfoo -lbar -lbaz 等来指定它们,但我认为您在之前的试用中错过了使用-L 指定路径。
    • 好的,现在它正在构建,但它立即崩溃。有什么想法吗?
    • @user2357505:这似乎与问题所问的问题不同,很高兴看到您解决了构建问题。至于崩溃,我认为您需要检查回溯以查看崩溃的位置。
    • 非常感谢您的帮助!我什至无法启动调试器。所以我认为这可能与那个问题有关?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多