我知道之前有人问过类似的问题,但看起来在类导出方面与简单函数有所不同......我已经检查了所有这些解决方案,检查了所有建议,但看起来我仍然是遗漏了一些东西......
发生了什么:
- 我有一个用 Visual Studio 2013 编写的主 C++ 项目,我想添加一个包含各种实用程序的 dll 库。我创建了一个虚拟的,基本上没有任何功能,但它无法编译:
2> TestSvc_i.c 2>TestSvc.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall CUtils::CUtils(void)" (__imp_??0CUtils@@QAE@XZ) referenced in function _wWinMain@16 2>C:\Work\TestSvc_root\Debug\TestSvc.exe : fatal error LNK1120: 1 unresolved externals ========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========
And the code in the main project looks like this:
extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
CUtils *a = new CUtils();
delete a;
return 1;
}
我有以下设置:
- 用 C++ 和 Visual Studio 2013 编写的主项目;
- 我使用的是 Unicode 字符集,运行时用于共享 dll;
_UNICODE
已定义;- 子系统:Windows (
/SUBSYSTEM:WINDOWS
); - 我想创建一个应该在共享 dll 中使用的实用程序库。代码如下所示:
实用程序.h:
#ifdef UTILS_EXPORTS
#define UTILS_API __declspec(dllexport)
#else
#define UTILS_API __declspec(dllimport)
#endif
// This class is exported from the Utils.dll
class UTILS_API CUtils {
public:
CUtils(void);
};
实用程序.cpp:
#include "stdafx.h"
#include "Utils.h"
// This is the constructor of a class that has been exported.
// see Utils.h for the class definition
CUtils::CUtils()
{
return;
}
- 我检查了 dll 项目设置,基本上,它们都与主项目中的一样;
UTILS_EXPORTS
通常在 dll-project 中定义,所有定义都应该有__declspec(dllexport)
(如预期的那样,未在UTILS_EXPORTS
中定义);- 我有以下预处理器定义:
WIN32
;_DEBUG
;_WINDOWS
;_USRDLL
;UTILS_EXPORTS
; - dll-project 被添加为 master-project 的依赖;
- dll 复制到
$(SolutionDir)\Debug
下,但不复制到$(SolutionDir)\$(MasterProject)\Debug
下 - 我不知道为什么。我手动复制了,还是一样的问题; - 我在 Utils.dll 上运行了
DUMPBIN
实用程序,它看起来是这样的:
C:\Work\TestSvc_root\Debug>DUMPBIN /EXPORTS /SYMBOLS Utils.dll Microsoft (R) COFF/PE Dumper Version 10.00.40219.01 Copyright (C) Microsoft Corporation. All rights reserved. Dump of file Utils.dll File Type: DLL Section contains the following exports for Utils.dll 00000000 characteristics 53C632A8 time date stamp Wed Jul 16 10:07:04 2014 0.00 version 1 ordinal base 2 number of functions 2 number of names ordinal hint RVA name 1 0 00011154 ??0CUtils@@QAE@XZ = @ILT+335(??0CUtils@@QAE@XZ) 2 1 000110C8 ??4CUtils@@QAEAAV0@ABV0@@Z = @ILT+195(??4CUtils@@QAEAAV0@ABV0@@Z) Summary 1000 .data 1000 .idata 2000 .rdata 1000 .reloc 1000 .rsrc 4000 .text 10000 .textbss
- 我尝试“嗅探”UTILS_API 的值:
#define DO_QUOTE(X) #X #define QUOTE(X) DO_QUOTE(X) #define MY_QUOTED_VAR QUOTE(MYVARIABLE) #pragma message(QUOTE(UTILS_API))
它转储正确的值:当从 dll 中“使用”时,它转储 __declspec(dllexport)
,当从主项目中使用时,它转储 __declspec(dllimport)
那么,有什么提示吗?看起来有些东西与类导出定义/方法修饰有关......在以前的普通旧 Visual C++ 中,曾经有一个“def”文件,你可以在其中映射导出的名称......但在这个新版本中,我迷路了。
谢谢!
最佳答案
作为解决方法,您可以将 .lib 文件(在构建 .dll 期间生成)显式添加到主项目的链接器选项中的输入库列表中。
关于c++ - Visual Studio 2013 dll 导出链接错误 (LNK2019/LNK1120),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24776853/