编译使用 dll 的代码时未解决的外部问题
我正在尝试开始在 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)” (函数 _main test.obj
dumpbin \exports testdllD.dll 中引用的 _imp_?printDLLFuncs@MyTestDll@TestDLLNS@@SAHXZ) 给出以下内容: 序号提示 RVA 名称
1 0 0001105F ?printDLLFuncs@MyTestDll@TestDLLNS@@SAHXZ
因此该符号显然存在于 .dll 中。 Visual Studio 是否还应该创建一个 testdllD.lib 文件,我应该将其与 test.cpp 链接?如果是这样,我如何让 Visual Studio 制作 .dll 和 .lib。
编辑:我的导入/导出正确吗?据我了解,在编译 dll 时,您需要使用 dllexport,而在编译使用 dll 的可执行文件时,将使用 dllimport。
I'm trying to get started on using dll's for my code in VS 2005. The code I have is very simple, just to try a test case.
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;
}
Error 1 error LNK2019: unresolved external symbol "__declspec(dllimport) public: static int _cdecl TestDLLNS::MyTestDll::printDLLFuncs(void)" (_imp_?printDLLFuncs@MyTestDll@TestDLLNS@@SAHXZ) referenced in function _main test.obj
dumpbin \exports testdllD.dll gives the following:
ordinal hint RVA name
1 0 0001105F ?printDLLFuncs@MyTestDll@TestDLLNS@@SAHXZ
So the symbol clearly exists in the .dll. Should Visual Studio also be creating a testdllD.lib files which I should be linking with test.cpp? If so, how I get visual studio to make both a .dll and a .lib.
Edit: Am I doing the importing/exporting correctly? From what I understand, whem compiling the dll you would want to use dllexport while when compiling the executable which uses the dll, dllimport would be used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几点需要注意,希望您没有错过。
There are few things to note and I hope you did not miss them.