该项目建立在 Win32 平台上,而不是在 x64 上。
完整错误信息: dllentry.obj : 错误 LNK2001: 未解析的外部符号“class CFactoryTemplate * g_Templates” (?g_Templates@@3PAVCFactoryTemplate@@A)
dllentry.cpp 在两个平台上编译。它包含外部声明:
extern CFactoryTemplate g_Templates[];
extern int g_cTemplates;
g_Templates[] 然后在两个函数中使用:
__control_entrypoint(DllExport) STDAPI DllGetClassObject(__in REFCLSID rClsID,
__in REFIID riid, __deref_out void **pv)
{
...
for (int i = 0; i < g_cTemplates; i++)
{
const CFactoryTemplate * pT = &g_Templates[i];
}
}
和
DllInitClasses(BOOL bLoading)
{
...
for (int i = 0; i < g_cTemplates; i++)
{
const CFactoryTemplate * pT = &g_Templates[i];
}
}
我检查了项目设置中的所有库,似乎一切正常,使用的是 64 位版本。我应该怎么做才能为 x64 平台构建项目?
最佳答案
您在对先前答案的评论中指出
The other, valid definition, is in myClass.cpp (main class of my project) 'CFactoryTemplate* g_Templates=0;' followed by 'int g_cTemplates=0;' at the uppermost level in the file, just after the includes.
您对 g_Templates
的定义与其声明不一致。
声明是
extern CFactoryTemplate g_Templates[];
这将 g_Templates
声明为 CFactoryTemplate
数组(大小未知)。但是你的定义
CFactoryTemplates* g_Templates = 0;
将其定义为指向 CFactoryTemplate
的指针,这不是一回事。
因此,您未能提供与声明匹配的 g_Templates
定义,错误消息是正确的。
您需要让声明和定义达成一致。一种方法是写
CFactoryTemplates g_Templates[1];
创建一个与声明一致的定义。 (请注意,零长度数组在 C++ 中是不允许的,因此我们创建一个长度为 1 的数组并忽略其中的元素。)
另一种是将声明改为
extern CFactoryTemplates* g_Templates;
关于c++ - LNK2001:为 x64 平台构建时无法解析的外部符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24700229/