名字装饰是C++编译器用来为函数和对象添加额外信息的方法。名字装饰有以下几种作用:
- 使链接器能够区分不同版本的重载函数。
- 使链接器能够检查对象和函数在所有模块中的声明方式是否完全相同。
- 使链接器能够在错误信息中提供关于未解析的引用类型的完整信息。
名字装饰是为了解决问题1而发明。附带解决了其他问题。
以如下一段代码为例:
int f(int i) { return i; }
int f(void) { return 0; }
int g(void) { return f() + f(1); }
windows
使用visual studio 编译上述代码得到 test.obj
首先通过 Tools > Command Line > Developer Command prompt 打开命令行窗口
> dumpbin /symbols test.obj
... 024 00000000 SECT4 notype () External | ?f@@YAHH@Z (int __cdecl f(int)) 025 00000000 SECT6 notype () External | ?f@@YAHXZ (int __cdecl f(void)) 026 00000000 SECT8 notype () External | ?g@@YAHXZ (int __cdecl g(void)) ...通过装饰后的名字查看未装饰名字
> undname ?f@@YAHH@Z Microsoft (R) C++ Name Undecorator Copyright (C) Microsoft Corporation. All rights reserved. Undecoration of :- "?f@@YAHH@Z" is :- "int __cdecl f(int)"Linux 查看装饰后的名字
$ g++ -c test.cpp $ nm test.o 0000000000000000 T _Z1fi 0000000000000010 T _Z1fv 000000000000001f T _Z1gv通过装饰后的名字查看未装饰名字
$ c++filt -n _Z1fi f(int)
参考:
- Decorated Names
- Name mangling
- Calling conventions



