Visual Studio 2013 dll导出联动错误(LNK2019 / LNK1120) [英] Visual Studio 2013 dll export linkage error (LNK2019/LNK1120)
问题描述
发生了什么:
- 我有一个主C ++在Visual Studio 2013中编写的项目,我想添加一个带有各种实用程序的dll库。我创建了一个虚拟的,基本上没有功能,但是它无法编译:
2> TestSvc_i.c
2> TestSvc.obj:error LNK2019:未解决的外部符号__declspec(dllimport)public:__thiscall CUtils :: CUtils(void)(__imp _ ?? 0CUtils @@ QAE @ XZ)在函数_wWinMain中引用@ 16
2> C:\Work\TestSvc_root\Debug\TestSvc.exe:致命错误LNK1120:1个未解决的外部
==========重建全部:1成功,1失败,0跳过==========
主项目中的代码如下所示:
externCint WINAPI _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nShowCmd)
{
CUtils * a = new CUtils();
删除a;
return 1;
}
我有以下设置:
- 使用Visual Studio 2013以C ++编写的主项目;
- 我使用的是Unicode字符集,运行时用于共享的dll; / li>
-
_UNICODE
被定义; - 子系统:Windows(
/ SUBSYSTEM :WINDOWS
); - 我想创建一个应用于共享DLL的实用程序库。代码如下所示:
Utils.h:
#ifdef UTILS_EXPORTS
#define UTILS_API __declspec(dllexport)
#else
#define UTILS_API __declspec(dllimport)
#endif
//这个类从Utils.dll
类UTILS_API CUtils {
public:
CUtils(void);
};
Utils.cpp:
#includestdafx.h
#includeUtils.h
//这是已经导出的类的构造函数。
//查看Utils.h的类定义
CUtils :: CUtils()
{
return;
}
- 我检查了dll项目设置,基本上,它们都在主项目中;
-
UTILS_EXPORTS
在dll项目中定义,所以通常所有定义都应该有__ declspec(dllexport)
(如预期,未在UTILS_EXPORTS
中定义); - 我具有以下预处理器定义:
WIN32
;_DEBUG
;_WINDOWS
;_USRDLL
;UTILS_EXPORTS
; - 添加dll项目作为依赖到主项目;
- 将dll复制到
$(SolutionDir)\Debug
下,但不在$(SolutionDir)\ $(MasterProject)\Debug
- 我不知道为什么。我手动复制它,但我仍然有同样的问题; - 我在Utils.dll上运行了
DUMPBIN
实用程序,这里是如何外观:
C:\Work\TestSvc_root\Debug> DUMPBIN / EXPORTS / SYMBOLS Utils .dll
Microsoft(R)COFF / PE Dumper版本10.00.40219.01
版权所有(C)Microsoft Corporation。版权所有。
文件的转储Utils.dll
文件类型:DLL
部分包含以下用于Utils.dll
00000000特征
53C632A8时间日期戳Wed Jul 16 10:07:04 2014
0.00版本
1 ordinal base
2函数数
2名字数
序数提示RVA名称
1 0 00011154?0CUtils @@ QAE @ XZ = @ ILT + 335(?0CUtils @@ QAE @ XZ)
2 1 000110C8 ?? 4CUtils @@ QAEAAV0 @ ABV0 @@ Z = @ ILT + 195(?4CUtils @@ QAEAAV0 @ ABV0 @@ Z)
摘要
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_QUO TE(X)
#define MY_QUOTED_VAR QUOTE(MYVARIABLE)
#pragma message(QUOTE(UTILS_API))
它转储正确的值:当从dllused转储 __ declspec(dllexport)
时,当从main-project使用它转储 __ declspec(dllimport)
所以,任何提示?它似乎有一些与类导出定义/方法装饰有关的东西...在以前的普通旧Visual C ++中,曾经是一个def文件,您可以在其中映射导出的名称...但在这个新版本,我迷路了。
谢谢!
,您可以将.lib文件(在.dll生成期间生成)显式添加到主项目的链接器选项中的输入库列表。
I know similar question has been asked before, but it looks like there is something different in what concerns classes export than a simple function ... I have checked all those solutions, checked all the suggestions but it still looks like I am missing something ...
What's happening:
- I have a master C++ project written in Visual Studio 2013 and I want to add a dll library with various utils. I have created a dummy one, with basically no functionality, but it fails to compile:
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;
}
I have the following setup:
- the master project written in C++ with Visual Studio 2013;
- I am using Unicode charset and the runtime is used in a shared dll;
_UNICODE
is defined;- SubSystem: Windows (
/SUBSYSTEM:WINDOWS
); - I want to create an utility library that should be used in a shared dll. The code looks like this:
Utils.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);
};
Utils.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;
}
- I checked the dll-project settings and basically, they're all as in main project;
UTILS_EXPORTS
is defined in dll-project so normally, all definitions should have__declspec(dllexport)
(as expected, not defined inUTILS_EXPORTS
);- I have the following preprocessor definitions:
WIN32
;_DEBUG
;_WINDOWS
;_USRDLL
;UTILS_EXPORTS
; - dll-project is added as a dependency to master-project;
- the dll is copied under
$(SolutionDir)\Debug
but not under$(SolutionDir)\$(MasterProject)\Debug
- I don't know why. I manually copied it, but I still have the same problem; - I ran
DUMPBIN
utility on Utils.dll and here is how it looks:
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
- I have tried to "sniff" the value of UTILS_API:
#define DO_QUOTE(X) #X #define QUOTE(X) DO_QUOTE(X) #define MY_QUOTED_VAR QUOTE(MYVARIABLE) #pragma message(QUOTE(UTILS_API))
which dumps the correct value: when "used" from dll, it dumps __declspec(dllexport)
, and when used from main-project it dumps __declspec(dllimport)
So, any hint? It looks like there is something that has to do with class export definition/method decoration ... In previous plain old Visual C++ there used to be a "def" file where you could map the exported names ... but in this new version, I am lost.
Thanks!
As a workaround, you may explicitly add .lib file (generated during the build of your .dll) to the list of input libraries in the linker options of the main project.
这篇关于Visual Studio 2013 dll导出联动错误(LNK2019 / LNK1120)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!