c++ - LNK2001:为 x64 平台构建时无法解析的外部符号

标签 c++ visual-c++ 64-bit lnk2001

该项目建立在 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/

相关文章:

c++ - 何时调用 C++ 析构函数?

c++ - 修改 Makefile 添加 GSL 库

c++ - 运算符临时重载

linux - 为不同的机器在网络文件系统下构建程序并安装软件

com - C# COM DLL : do I use Regasm, 或 Regsvr32?

C#/命令行界面 : Destructor not called if Dispose() used in it

c++ - 通过函数中的编译时变量访问 C++11 中的元组元素

visual-c++ - 是否有一种干净的方法来处理 C++ COM 对象中抛出的 bad_alloc 异常?

c - 无法理解的VC++6编译错误C2664

windows - 64位Windows平台还不成熟吗? (即使将 32 位二进制文​​件与在其上运行的 64 位二进制文​​件进行比较)