cha*_*r m 4 c c++ visual-studio-2013
我将所有本机库链接到WPF应用程序中使用的.dll.
我已经完成了与其他项目编译到库,但最新的项目不能以某种方式工作,虽然所有似乎都是相同的方式.我喜欢这样:
.H:
#ifndef MYHEADER_H_
#define MYHEADER_H_
#ifdef __cplusplus
extern "C" {
#endif
void MySetLoginResultCallback(int(*Callback)(int Ok, const char *UserName));
#ifdef __cplusplus
} // end of extern "C"
#endif
#endif // MYHEADER_H_
Run Code Online (Sandbox Code Playgroud)
的.cpp:
typedef int(*LoginResultCB_t)(int IsOk, const char *UserName);
LoginResultCB_t gLoginResultCB;
void MySetLoginResultCallback(LoginResultCB_t pCB)
{
gLoginResultCB = pCB;
}
extern "C" __declspec(dllexport) int MyLoginResultCB(int Ok, cons char *UserName)
{
if (gLoginResultCB)
return gLoginResultCB(Ok, UserName);
return -1;
}
Run Code Online (Sandbox Code Playgroud)
MyLoginResultCB导入到WPF exe并从那里调用.在初始化中,从本机.dll中的C文件调用MySetLoginResultCallback.
在.dll链接中,我从MySetLoginResultCallback(在本机.c文件中调用)中得到未解决的错误.如果我保持标题完全相同并重命名.cpp - > .c并删除extern"C",则.dll链接会成功.我在这里错过了什么?
来自aini.c的电话
MySetLoginResultCallback(XpAfterLoginCB);
Run Code Online (Sandbox Code Playgroud)
错误:
1> aini.obj:错误LNK2019:函数_InitNoAKit中引用的未解析的外部符号_MySetLoginResultCallback
在.cpp文件中,您将MySetLoginResultCallback使用C++语言链接定义一个函数.这与.h文件中声明的C语言链接的函数不同MySetLoginResultCallback.
正确的解决方案是将.cpp语言链接添加到.cpp文件中:
extern "C" {
typedef int(*LoginResultCB_t)(int IsOk, const char *UserName);
LoginResultCB_t gLoginResultCB;
void MySetLoginResultCallback(LoginResultCB_t pCB)
{
gLoginResultCB = pCB;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,函数类型也具有语言链接,这意味着LoginResultCB_t必须在.cpp文件中使用C语言链接声明typedef ,因为该参数在.h文件中声明为此类.