【发布时间】:2011-08-13 02:13:54
【问题描述】:
我正在尝试开始在 VS 2005 中为我的代码使用 dll。我的代码非常简单,只是为了尝试一个测试用例。
testdll.h:
#ifdef TEST_EXPORTS
#define TESTDLLPORT __declspec( dllexport )
#else
#define TESTDLLPORT __declspec( dllimport )
#endif
namespace TestDLLNS
{
static int s = 0;
class MyTestDll {
public:
static TESTDLLPORT int printDLLFuncs();
};
}
testdll.cpp:
// testdll.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "testdll.h"
#ifdef _MANAGED
#pragma managed(push, off)
#endif
namespace TestDLLNS {
int MyTestDll::printDLLFuncs() {
cout << "DLL function called" << endl;
return s;
}
}
#ifdef _MANAGED
#pragma managed(pop)
#endif
test.cpp:
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "testdll.h"
int main(int argc, char* argv[])
{
cout << "int: " << TestDLLNS::MyTestDll::printDLLFuncs() << endl;
cout << "Called dll" << endl;
return 0;
}
错误 1 错误 LNK2019:无法解析的外部符号“__declspec(dllimport) public: static int _cdecl TestDLLNS::MyTestDll::printDLLFuncs(void)”(_imp_?printDLLFuncs@MyTestDll@TestDLLNS@@ SAHXZ) 在函数 _main test.obj 中引用
dumpbin \exports testdllD.dll 给出以下内容: 序号提示 RVA 名称
1 0 0001105F ?printDLLFuncs@MyTestDll@TestDLLNS@@SAHXZ
所以符号显然存在于 .dll 中。 Visual Studio 是否也应该创建一个我应该与 test.cpp 链接的 testdllD.lib 文件?如果是这样,我如何让 Visual Studio 同时制作 .dll 和 .lib。
编辑:我是否正确地进行了导入/导出?据我了解,在编译使用 dll 的可执行文件时,您将要使用 dllexport 编译 dll,而将使用 dllimport。
【问题讨论】:
-
您是否在一个解决方案中拥有 TestDll 项目和 Test 项目?
-
是的,它们在同一个解决方案中,并且共享同一个输出调试目录。
-
当两个项目都在一个解决方案中时,您不需要摆弄库路径。您可以设置将为您完成这项工作的项目依赖项。
标签: c++ visual-studio dll dynamic-linking