我正在尝试在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是否也应该创建一个testdllD.lib文件,我应该与test.cpp链接?如果是这样,我如何让visual studio同时制作.dll和.lib .
编辑:我正在进行正确的导入/导出吗?根据我的理解,在编译使用dll的可执行文件时,编译dll你想要使用dllexport,将使用dllimport .
1 回答
有几点需要注意,我希望你不要错过它们 .
您所拥有的是链接错误,意味着您的编译没问题,但是存在链接错误 . 该错误显示链接未找到对象文件中引用的printDLLFuncs()函数的定义 .
您必须提供.lib文件的路径,其中函数在项目目录 - >库路径中定义,或者将其放在任何项目文件夹中,以便Visual Studio可以找到它 .