我不是Visual Studio的新手,我知道有很多问题。但我真的无法弄清楚这一点。这是错误:
1>moc_displaycounter.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const Counter::staticMetaObject" (?staticMetaObject@Counter@@2UQMetaObject@@B)
1>moc_displaymanometer.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const ManoMeter::staticMetaObject" (?staticMetaObject@ManoMeter@@2UQMetaObject@@B)
1>moc_displaysvgmeter.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const SVGMeter::staticMetaObject" (?staticMetaObject@SVGMeter@@2UQMetaObject@@B)
所以我在导入库上做了一个dumpbin / EXPORTS,它返回了这个:
Microsoft (R) COFF/PE Dumper Version 14.00.24210.0
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file analogwidgets.lib
File Type: LIBRARY
Exports
ordinal name
??0AbstractMeter@@QEAA@PEAVQWidget@@@Z (public: __cdecl AbstractMeter::AbstractMeter(class QWidget *))
?staticMetaObject@Counter@@2UQMetaObject@@B (public: static struct QMetaObject const Counter::staticMetaObject)
?staticMetaObject@Item@@2UQMetaObject@@B (public: static struct QMetaObject const Item::staticMetaObject)
?staticMetaObject@Led@@2UQMetaObject@@B (public: static struct QMetaObject const Led::staticMetaObject)
?staticMetaObject@ManoMeter@@2UQMetaObject@@B (public: static struct QMetaObject const ManoMeter::staticMetaObject)
?staticMetaObject@PotentioMeter@@2UQMetaObject@@B (public: static struct QMetaObject const PotentioMeter::staticMetaObject)
?staticMetaObject@SVGMeter@@2UQMetaObject@@B (public: static struct QMetaObject const SVGMeter::staticMetaObject)
我检查了导入库上的确切签名。为了更好的衡量,我使用 depends 来检查这些符号是否在DLL中(是的,我知道它不会在链接过程中起作用,但只是为了确保导入库没有破坏),但它仍然没有链接。尝试使用调试和发布x64构建但没有成功。以前没有其他版本的VS的问题。
关于我接下来应该做什么的任何想法?
答案 0 :(得分:2)
从DLL导出静态数据有点棘手。链接器错误表示您忘记执行的操作,在客户端代码中使用时,类未声明为__declspec(dllimport)。 Repro代码:
Header.h:
#ifdef _USRDLL
#define EXPORTED __declspec(dllexport)
#else
#define EXPORTED //__declspec(dllimport) // <=== Problem here!
#endif
struct EXPORTED QMetaObject {
public:
int foo;
};
class EXPORTED Counter {
public:
static const QMetaObject staticMetaObject;
};
Source.cpp:
#include "header.h"
const QMetaObject Counter::staticMetaObject;
Client.cpp:
#include "Header.h"
int main()
{
auto foo = Counter::staticMetaObject.foo;
return 0;
}
链接错误:
ConsoleApplication1.obj:错误LNK2001:未解析的外部符号“public:static struct QMetaObject const Counter :: staticMetaObject”(?staticMetaObject @ Counter @@ 2UQMetaObject @@ B)
删除Header.h中的//注释以进行修复,编译器现在知道如何正确使用指向DLL数据的指针。请注意原始链接器错误如何告诉您有关此问题的信息。如果现在在库上运行Dumpbin.exe / exports,则可以看到导出的成员名称已更改为__imp_?staticMetaObject@Counter@@2UQMetaObject@@B
。哪个是指向数据的指针。指针是必需的,因为如果无法在其首选基址加载DLL,则可以重新定位数据。