Qt 5.1 - 构建时出现"Add library"错误。LNK2019

Qt 5.1 - "Add library" error on build. LNK2019

本文关键字:Add library 错误 LNK2019 构建 Qt      更新时间:2023-10-16

我正在尝试将c ++库(在Qt中创建)链接到控制台应用程序。在纯 c++ 中,我确实成功地将库 dll 链接到程序。在Qt中,我得到LNK2019:未解析的外部符号。

图书馆

利巴

*liba_global.h*

#ifndef LIBA_GLOBAL_H
#define LIBA_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(LIBA_LIBRARY)
#  define LIBASHARED_EXPORT Q_DECL_EXPORT
#else
#  define LIBASHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // LIBA_GLOBAL_H

*lib_example.h*

#ifndef LIB_EXAMPLE_H
#define LIB_EXAMPLE_H
#include "liba_global.h"
    //Interface as seen on http://www.tutorialspoint.com/cplusplus/cpp_interfaces.htm
    class LIBASHARED_EXPORT Lib_example
    {
    public:
        Lib_example(){}
        virtual ~Lib_example(){}
        virtual int getId()=0;
        virtual void setId(int id)=0;
    };
#endif // LIB_EXAMPLE_H

*test_a.h*

#ifndef TEST_A_H
#define TEST_A_H
#include "liba_global.h"
#include "lib_example.h"
class LIBASHARED_EXPORT Test_A:public Lib_example
{
public:
    Test_A();
    ~Test_A();
    int getId();
    void setId(int id);
private:
    int id;
};
#endif // TEST_A_H

*test_a.cpp*

#include "test_a.h"
int Test_A::getId()
{
    return id;
}
void Test_A::setId(int id)
{
    this->id = id;
}
Test_A::Test_A()
{
    this->id = 0;
}
Test_A::~Test_A()
{ }

QT       -= gui
TARGET = LibA
TEMPLATE = lib
DEFINES += LIBA_LIBRARY
SOURCES += 
    test_a.cpp
HEADERS += lib_example.h
        liba_global.h 
    test_a.h
unix:!symbian {
    maemo5 {
        target.path = /opt/usr/lib
    } else {
        target.path = /usr/lib
    }
    INSTALLS += target
}

项目

主.cpp

#include <QCoreApplication>
#include <iostream>
#include "test_a.h"
using namespace std;
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Test_A test;
    test.setId(2);
    cout<<test.getId()<<endl;
    return a.exec();
}

QT       += core
QT       -= gui
TARGET = ProjUsingLib_A
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app

SOURCES += main.cpp
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../build-LibA-Desktop_Qt_5_1_1_MSVC2012_32bit-Debug/release/ -lLibA
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../build-LibA-Desktop_Qt_5_1_1_MSVC2012_32bit-Debug/debug/ -lLibA
else:unix: LIBS += -L$$PWD/../build-LibA-Desktop_Qt_5_1_1_MSVC2012_32bit-Debug/ -lLibA
INCLUDEPATH += $$PWD/../LibA
DEPENDPATH += $$PWD/../LibA

我编写了代码并使用Qt Creator进行链接。

更多详细信息:使用 MSVC2012_32bit_debug 选项编译的 lib 和 main。

错误:

main.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall Test_A::Test_A(void)" (??0Test_A@@QAE@XZ) referenced in function _main
main.obj:-1: error: LNK2019: unresolved external symbol "public: virtual __thiscall Test_A::~Test_A(void)" (??1Test_A@@UAE@XZ) referenced in function _main
main.obj:-1: error: LNK2019: unresolved external symbol "public: virtual int __thiscall Test_A::getId(void)" (?getId@Test_A@@UAEHXZ) referenced in function _main
main.obj:-1: error: LNK2019: unresolved external symbol "public: virtual void __thiscall Test_A::setId(int)" (?setId@Test_A@@UAEXH@Z) referenced in function _main

提前致谢:)

你不是在构建/链接test_a.cpp。 将其添加到生成文件的SOURCES行。