我有一个C ++ Visual Studio 2013控制台应用程序,该应用程序应该利用我编写的DLL MyDLLlib.dll。MyDLLlib用C编写。函数之一称为Get_Version。原型是
const char *Get_Version();
我将其放在源文件的顶部以利用原型:
extern "C"{
#include "MyDLLlib.h"
}
如果在函数中这样在主体中调用
printf("version %s\n",Get_Version());
然后就可以了。
但是,如果我添加了一个带有一些静态方法的类,并且一个静态方法调用了Get_Version()
const char * ret = Get_Version();
然后我得到一个链接错误:
Error 1 error LNK2019: unresolved external symbol
"__declspec(dllimport) char * __cdecl Get_Version(void)" (__imp_?Get_Version@@YAPADXZ)
referenced in function "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl ServiceDispatch::decoder_Get_Version(class StringBuffer &)"
(?decoder_Get_Version@ServiceDispatch@@CA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AAVStringBuffer@@@Z)
D:\devt\CplusPlus\VSTrials\Link_to_MyDLLlib\Link_to_MyDllLib\ServiceDispatch.obj Link_to_MyDLLlib``
我正在使用相同的包含。
关于我可能做错了什么的任何线索?
如果您始终将CLASS_DECLSPEC
定义为,那么这肯定无法正常工作。看这个例子:__declspec(dllimport)
DLL_header.h
#if defined( _BUILD_DLL )
# define DLLAPI __declspec(dllexport) //Export when building DLL
#else
# define DLLAPI __declspec(dllimport) //Import when using in other project
#endif
DLLAPI const char *Get_Version();
DLL_source.cpp
#include "Header.h"
const char *Get_Version()
{
return "1.1.0.4";
}
用_BUILD_DLL
定义的方式生成DLL 。
Main.cpp
#include "DLL_header.h"
int main()
{
printf("%s\n", Get_Version());
return 0;
}
用_BUILD_DLL
未定义的方式构建它。
在您的情况下,可能会出现问题extern "C"
-您在标头中包含标头extern "C"
,该标头声明Get_Version()
具有__cdecl
链接。但是链接器正在搜索
__imp_?Get_Version@@YAPADXZ
这是一个错误的(C ++)名称。您的DLL是C还是C ++项目?如果您的DLL是作为C
项目构建的(不是C++
),请extern "C"
在Get_Version()
的声明中加上以下内容#ifdef
:
#ifdef __cplusplus
extern "C" {
#endif
DLLAPI const char *Get_Version();
#ifdef __cplusplus
}
#endif
无论哪种方式,都请extern "C"
从中删除#include
。另外,检查.lib
此DLL的文件是否作为依赖项附加到项目。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句