我正在尝试将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
}
的main.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
提前致谢:)
答案 0 :(得分:0)
您没有建立/链接test_a.cpp
。将其添加到makefile的SOURCES
行。