0

我正在尝试将 c++ 库(在 Qt 中创建)链接到控制台应用程序。在纯 c++ 中,我确实成功地将库 dll 链接到程序。在 Qt 中,我得到了 LNK2019: unresolved external symbol。

图书馆

LibA

标题

*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
}

项目

主文件

#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

提前致谢 :)

4

1 回答 1

0

您不是在构建/链接test_a.cpp. 将其添加到SOURCES您的 makefile 行。

于 2013-09-12T14:24:16.723 回答