当我尝试使用在Qt Creater和Qt 5.1中使用VS2010编译的静态库时,我遇到了问题。我正在使用 qt5.1。它是使用 VS2010 编译器编译的。
我的简单库的源代码如下所示:
Lib_Test.h
#pragma once
#include <iostream>
class Lib_Test
{
public:
Lib_Test(void);
~Lib_Test(void);
void HelloTest();
};
Lib_Test.cpp
#include "Lib_Test.h"
Lib_Test::Lib_Test(void)
{
}
Lib_Test::~Lib_Test(void)
{
}
void Lib_Test::HelloTest()
{
std::cout << "Hello World!";
}
这两个文件被编译到我的"Lib_Test.lib"中。我将库和头文件复制到"C:/Qt/"以简化库调用。我的qt项目文件(用于c ++控制台应用程序):
QT += core
QT -= gui
TARGET = Lib_Test_Qt
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += C:/Qt
DEPENDPATH += C:/Qt
win32:CONFIG(release, debug|release): LIBS += -LC:/Qt/
win32:CONFIG(release, debug|release): LIBS += -lLIB_Test
最后是主要的.cpp
#include <QCoreApplication>
#include <Lib_Test.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Lib_Test *lb = new Lib_Test();
lb->HelloTest();
return a.exec();
}
当我尝试在Qt Creator中构建项目时,我收到以下错误消息
main.obj:-1: Fehler:LNK2019: unresolved external symbol "public: void __thiscall Lib_Test::HelloTest(void)" (?HelloTest@Lib_Test@@QAEXXZ) referenced in function _main
main.obj:-1: Fehler:LNK2019: unresolved external symbol "public: __thiscall Lib_Test::Lib_Test(void)" (??0Lib_Test@@QAE@XZ) referenced in function _main
debugLib_Test_Qt.exe:-1: Fehler:LNK1120: 2 unresolved externals
当我将 HelloTest 方法声明为静态方法并尝试在不创建 Lib_Test 实例的情况下调用它时,我收到类似的错误消息
main.obj:-1: Fehler:LNK2019: unresolved external symbol "public: static void __cdecl Lib_Test::HelloTest(void)" (?HelloTest@Lib_Test@@SAXXZ) referenced in function _main
debugLib_Test_Qt.exe:-1: Fehler:LNK1120: 1 unresolved externals
我错过了什么?有人能帮忙吗?现在真的很令人沮丧:/。
编辑:
我在msvs2010控制台中尝试了DUMPBIN/SYMBOL Lib_Test.lib,我得到的只是:
Microsoft (R) COSS/PE Dumper Version 10.00.40210.01
Copytight (C) Microsoft Corporation. All right reserved
Dump of file Lib_Test.lib
File Type: LIBRARY
这是否意味着我的库以某种方式是空的?? :/
在你的 qmake 文件中,更改 win32:CONFIG(release, debug|release): LIBS += -C:/Qt/-lLib_Test win32:CONFIG(release, debug|release): PRE_TARGETDEPS += C:/Qt/Lib_Test.lib
自
win32:CONFIG(release, debug|release): LIBS += -LC:/Qt/
win32:CONFIG(release, debug|release): LIBS += -lLib_Test
或
win32:CONFIG(release, debug|release): LIBS += -LC:/Qt/
win32:CONFIG(release, debug|release): LIBS += -l_Test
我认为其中一个应该有效。不要忘记转到"构建"菜单,然后运行 qmake。