有A,B两个动态库,其中A动态库定义了定义了一个函数:
__declspec(dllexport) int get_fun_info(char const * para1, ulonglong para2, unsigned char para3, class QList<Cond * > * para4, char * para5, bool para6, bool para7)
B模块引用了该函数,但编译过程链接失败,提示下面符号
?get_fun_info@@YAHPEBD_KEPEAV?$QList@PEAVCond@@@@PEAD_N4@
查找符号出错。查看A动态库的lib文件,在其中找到了对应函数的符号为:
?get_fun_info@@YAHPEBD_KEPEAV?$QList@PEAUCond@@@@PEAD_N4@Z
显然A动态库导出的符号与B模块需要查找的符号名称有差异。
在 Windows 的DbgHelp.dll 导出函数中,UnDecorateSymbolName 是用于解析 Name Mangling 字符串的,具体函数的细节可以查看 MSDN:unDecorateSymbolName 函数 (dbghelp.h) - Win32 apps | Microsoft Learn
通过下面方式解析符号,可以分别获得对应的函数原型:
void UnDecorateName(char szDecorateName[])
{
char szUnDecorateName[2048] = {0};
printf("The DecorateName Name Is: %s \n", szDecorateName);
if (UnDecorateSymbolName(szDecorateName, szUnDecorateName, sizeof(szUnDecorateName), UNDNAME_COMPLETE) == 0)
{
printf("UnDecorateSymbolName Failed. GetLastError() = %d \n", GetLastError());
return;
}
printf("The UnDecorated Name Is: %s \n", szUnDecorateName);
}
char str1[] = "?get_fun_info@@YAHPEBD_KEPEAV?$QList@PEAVCond@@@@PEAD_N4@Z";
char str2[] = "?get_fun_info@@YAHPEBD_KEPEAV?$QList@PEAUCond@@@@PEAD_N4@Z";
UnDecorateName(str1);
UnDecorateName(str2);
从打印信息可以发现:
The DecorateName Name Is: ?get_fun_info@@YAHPEBD_KEPEAV?$QList@PEAVCond@@@@PEAD_N4@Z
The UnDecorated Name Is: int __cdecl get_fun_info(char const * __ptr64,unsigned __int64,unsigned char,class QList<class Cond * __ptr64> * __ptr64,char * __ptr64,bool,bool)
The DecorateName Name Is: ?get_fun_info@@YAHPEBD_KEPEAV?$QList@PEAUCond@@@@PEAD_N4@Z
The UnDecorated Name Is: int __cdecl get_fun_info(char const * __ptr64,unsigned __int64,unsigned char,class QList<struct Cond * __ptr64> * __ptr64,char * __ptr64,bool,bool)
原型差异在class Cond与struct Cond的上面,检查代码发现:
A模块中使用struct 定义的Cond类型;而B模块在起头文件中采用了class Cond的前置声明,与A模块实际定义的不一致,导致B模块链接失败。
vc++编译器中,对虽然大部分情况struct和class效果类似,但不是完全一样的。
l另外,本文中涉及到VC环境中C++ 语言中的 Name Mangling,可以参考
Decorated names | Microsoft Learn
解析VC++ Name Mangling 机制_c++name manling原理-CSDN博客 Name Mangling解析VC++ Name Mangling 机制_c++name manling原理-CSDN博客