# 名称修饰

名称修饰（name mangling），又称名称装饰（name decoration），是编译器将函数、变量或对象的源名称，连同其参数类型、类或命名空间作用域、调用约定等信息，编码为一个单一符号名的技术，以便链接器用该符号名匹配定义与引用。之所以需要这项技术，是因为目标文件的符号表和经典链接器只能理解扁平的标识符字符串，而 C++ 等语言允许许多实体共用一个源名称：重载函数、不同类或命名空间的成员、模板实例化，都需要各自不同的符号。<sup>[1](https://learn.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170)</sup><sup> • </sup><sup>[2](https://www.cl.cam.ac.uk/~pes20/rems/elf-linking-2016.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 目的 | 将调用约定、类型、函数参数和作用域编码进符号名，使链接器能找到正确的函数和对象<sup>[1](https://learn.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170)</sup> |
| C++（Itanium ABI） | 名称修饰的方法是在名称的编码前加 `_Z`，对于函数还要加上其类型；修饰方案的设计目标是：当且仅当两个实体在单一定义规则下是同一实体时，它们才获得相同的修饰名<sup>[3](https://itanium-cxx-abi.github.io/cxx-abi/abi.html)</sup> |
| C++（MSVC） | 修饰名以 `?` 开头，编码类、命名空间、参数类型、调用约定和返回类型；例如 `void __stdcall b::c(float)` 变为 `?c@b@@AAGXM@Z`<sup>[1](https://learn.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170)</sup> |
| C 函数 | 装饰方式取决于调用约定：`__cdecl` 加一个前导下划线，`__stdcall` 加一个下划线并在末尾加 `@` 和参数列表的字节数；在 64 位 MSVC 中，C 函数仅在使用 `__vectorcall` 时才有修饰<sup>[1](https://learn.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170)</sup> |
| Rust v0 | 符号以 `_R` 开头，仅使用 `A-Z`、`a-z`、`0-9` 和 `_`，可逆地编码泛型参数，且不打算与其他修饰方案兼容<sup>[4](https://dev-doc.rust-lang.org/stable/rustc/symbol-mangling/v0.html)</sup> |
| 反修饰 | 工具可逆向还原编码：Windows 上有 `undname.exe` 和 `UnDecorateSymbolName`，针对 Itanium 修饰名有 `c++filt`、`llvm-cxxfilt` 和 `abi::__cxa_demangle`<sup>[1](https://learn.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170)</sup><sup> • </sup><sup>[5](https://learn.microsoft.com/en-us/windows/win32/debug/decorated-symbol-names)</sup><sup> • </sup><sup>[6](https://gcc.gnu.org/onlinedocs/gcc-12.2.0/libstdc%2B%2B/manual/manual/ext_demangling.html)</sup> |
| 兼容性影响 | 不同编译器有意采用不同的修饰方案，因此不兼容的 C++ 库会在链接时失败，而不是在运行时崩溃<sup>[7](https://gcc.gnu.org/onlinedocs/gcc/Interoperation.html)</sup> |

## 为什么需要名称修饰

ELF 符号名是任意非零字节序列，但大多数实现将符号限制在汇编器所能接受的较窄名称集合内。C 标识符在设计上恰好符合该集合；C++ 名称包含 `::` 等标点符号，对汇编器不友好，这是 C++ ABI 定义名称修饰方案的第一个原因。<sup>[2](https://www.cl.cam.ac.uk/~pes20/rems/elf-linking-2016.pdf)</sup> 第二个原因是重载：若干名为 `f` 的函数只要参数类型不同，就可以共存于同一程序中，链接器需要用唯一的符号来识别每个函数。将参数类型编码进符号名，就使每个重载、类成员、命名空间成员和模板实例化都获得了自己的身份。<sup>[1](https://learn.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170)</sup>

Itanium C++ ABI 精确地表述了设计目标：名称修饰的设计使得当且仅当两个实体在 C++ 标准的单一定义规则和声明匹配规则下是同一实体时，它们才获得相同的修饰名。由于这些语言规则很复杂，修饰规则也很复杂，而且 ABI 必须随语言一同演进，以保证新的语言构造可以被编码。<sup>[3](https://itanium-cxx-abi.github.io/cxx-abi/abi.html)</sup>

## 修饰名里包含什么

对于 C++ 函数，MSVC 修饰名记录函数名、所属类（包括嵌套类）、命名空间、参数类型、调用约定、返回类型，以及一个可选的特定目标元素，例如 ARM64EC 目标文件中插入的 `$$h` 标记。<sup>[1](https://learn.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170)</sup> 在 Itanium ABI 下，编码是在名称前加 `_Z`，对于函数还要加上类型；对于函数模板实例化，所编码的类型是模板中表达的类型，而对于变量和静态数据成员则省略类型。<sup>[3](https://itanium-cxx-abi.github.io/cxx-abi/abi.html)</sup>

**C 函数的装饰较轻。** 在 MSVC 中，形式取决于调用约定：`__cdecl`（默认）加一个前导下划线；`__stdcall` 加一个前导下划线，并在末尾加 `@` 和参数列表字节数的十进制数字；`__fastcall` 使用前导和尾随的 `@` 符号并附带字节数；`__vectorcall` 使用两个尾随 `@` 符号。在 64 位环境中，C 函数或 `extern "C"` 函数仅在使用 `__vectorcall` 时才有修饰。同样的 C 风格装饰也适用于以 `extern "C"` 链接声明的 C++ 代码。<sup>[1](https://learn.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170)</sup> Win32 调试文档给出了一个熟悉的例子：`__stdcall` 函数 `LoadLibrary` 的修饰名为 `LoadLibrary@4`，末尾的 4 是其单个指针参数的字节数。<sup>[5](https://learn.microsoft.com/en-us/windows/win32/debug/decorated-symbol-names)</sup>

**其他语言使用自己的约定。** [GNU Fortran](https://www.edgepedia.org/zh/gnu-fortran) 默认输出小写的 Fortran 名称并附加一个下划线；模块实体命名为 `__` 后接小写模块名、`_MOD_` 和小写 Fortran 名，末尾不加下划线。`-fno-underscoring` 和 `-fsecond-underscore` 等选项会改变这一行为；在 32 位 Windows 上使用 `stdcall` 时，会附加一个 `@` 符号和一个整数。<sup>[8](https://gcc.gnu.org/onlinedocs/gfortran/Naming-conventions.html)</sup> Java 本地接口定义了从 `native` 方法到 C 函数名的一一映射：前缀 `Java_`、转义后的类名、一个下划线、转义后的方法名；当本地方法被重载时，再加两个下划线和一个转义后的参数描述符，例如 `Java_p_q_r_A_f__ILjava_lang_String_2`。<sup>[9](https://docs.oracle.com/en/java/javase/25/docs/specs/jni/design.html)</sup>

## 主要的名称修饰方案

**Itanium C++ ABI** 是微软工具链之外的主流方案。它最初是为 64 位安腾架构编写的厂商中立规范，但其中通用部分适用于任何平台；GCC 自 3.2 版起将其 C++ 二进制约定建立在此之上，其他厂商也在 GNU/Linux 和 BSD 系统等平台上实现它。<sup>[10](https://gcc.gnu.org/onlinedocs/gcc/Compatibility.html)</sup> RISC-V 已批准的 ABI 规范直接采用 Itanium C++ 名称修饰，没有 RISC-V 特有的规则。<sup>[11](https://docs.riscv.org/reference/abi/riscv-elf-cpp-name-mangling.html)</sup>

**MSVC 使用自己的方案**，其中 C++ 修饰名以 `?` 开头，编码的函数名和类名之后的部分是一个仅对编译器和链接器有内部意义的代码。<sup>[1](https://learn.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170)</sup>

**Rust 的 v0 格式**由 RFC 2603 引入，每个修饰符号以两个字符 `_R` 开头，后面可选地跟一个版本号（目前未使用），然后是实体的路径、可选的实例化 crate 消歧符，以及可选的厂商特定后缀。它为二进制符号表中可能出现的所有内容提供了无歧义的编码，可逆地编码泛型参数，将符号限制为 `A-Z`、`a-z`、`0-9` 和 `_` 以保证平台独立性，并明确不打算与 C++ 等其他修饰方案兼容。它也不作为 Rust 的稳定 ABI 提出；其定义足够完善，使反修饰器能生成可读形式。<sup>[4](https://dev-doc.rust-lang.org/stable/rustc/symbol-mangling/v0.html)</sup>

## 名称修饰与 ABI 兼容性

修饰名是编译器应用二进制接口的一部分，因此改变修饰方案会破坏与先前编译的库的二进制兼容性。GCC 通过 `-fabi-version=n` 选项暴露这一点：版本 0 最贴近 C++ ABI 规范，并随 ABI 缺陷修复而变化；版本 1 首次出现于 G++ 3.2；版本 2 是直到 G++ 4.9（含）为止的默认值；后续版本修正了特定构造的修饰，如向量类型、`nullptr_t`、lambda 和结构化绑定。<sup>[12](https://gcc.gnu.org/onlinedocs/gcc-15.1.0/gcc/C_002b_002b-Dialect-Options.html)</sup> 在可用强别名的情况下，G++ 会补偿修饰的变化：当定义了一个带有错误修饰名的符号时，它还会发出一个携带正确修饰名的别名，并通过 `-fabi-compat-version` 选择使用哪个别名。<sup>[12](https://gcc.gnu.org/onlinedocs/gcc-15.1.0/gcc/C_002b_002b-Dialect-Options.html)</sup>

编译器之间的差异是有意为之。GCC 的文档解释说，编译器在类布局、多重继承和虚函数分派方面存在差异；如果各编译器的名称编码变得相同，程序就会链接到不兼容的库并在运行时崩溃；不同的修饰方案反而能在链接时检测出不兼容。<sup>[7](https://gcc.gnu.org/onlinedocs/gcc/Interoperation.html)</sup> 实际后果是，C 和 C++ 库及 DLL 应使用相同的编译器工具集、标志和目标架构编译；与 MSVC 编译的源文件链接的汇编代码必须使用 MSVC 修饰名和调用约定。<sup>[1](https://learn.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170)</sup>

**为互操作性而规避名称修饰。** 必须能被 C、Fortran 或其他语言调用的 C++ 代码，用 `extern "C"` 链接声明函数，这会抑制 C++ 名称修饰，转而应用平台的 C 装饰规则。<sup>[1](https://learn.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170)</sup> Fortran 2003 标准化了 `BIND(C)` 属性，它告诉编译器某个符号应与 C 互操作。<sup>[13](https://gcc.gnu.org/onlinedocs/gfortran/Interoperability-with-C.html)</sup>

## 查看与反修饰

修饰名是程序员通常看不到的实现细节，但在向链接器指定符号或阅读工具输出时会浮现出来，例如在匹配重载函数、命名空间成员、构造函数或析构函数时。改变函数的名称、类、调用约定、返回类型或参数，都会改变修饰名。<sup>[1](https://learn.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170)</sup>

工具可以逆向还原编码。在 Windows 上，`undname.exe` 将修饰名转换为可读形式，把 `?func1@a@@AAEXH@Z` 变成 `private: void __thiscall a::func1(int)`，`UnDecorateSymbolName` API 则以编程方式完成同样的工作。<sup>[1](https://learn.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170)</sup><sup> • </sup><sup>[5](https://learn.microsoft.com/en-us/windows/win32/debug/decorated-symbol-names)</sup> 在 GNU 工具链中，将 C++ ABI 标识符还原为源标识符称为反修饰（demangling），`abi::__cxa_demangle` 函数向程序提供这一能力，而 `c++filt` 和链接器等工具则自行解码名称。<sup>[6](https://gcc.gnu.org/onlinedocs/gcc-12.2.0/libstdc%2B%2B/manual/manual/ext_demangling.html)</sup>

## References

1. [Decorated names, Microsoft Learn](https://learn.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170)
2. [Name mangling in ELF linking, Rems project, University of Cambridge](https://www.cl.cam.ac.uk/~pes20/rems/elf-linking-2016.pdf)
3. [Itanium C++ ABI, mangling section](https://itanium-cxx-abi.github.io/cxx-abi/abi.html)
4. [v0 Symbol Format, The rustc book](https://dev-doc.rust-lang.org/stable/rustc/symbol-mangling/v0.html)
5. [Decorated Symbol Names, Win32 apps, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/debug/decorated-symbol-names)
6. [Chapter 28. Demangling, GNU libstdc++ manual](https://gcc.gnu.org/onlinedocs/gcc-12.2.0/libstdc%2B%2B/manual/manual/ext_demangling.html)
7. [Interoperation, Using the GNU Compiler Collection (GCC)](https://gcc.gnu.org/onlinedocs/gcc/Interoperation.html)
8. [Naming conventions, The GNU Fortran Compiler](https://gcc.gnu.org/onlinedocs/gfortran/Naming-conventions.html)
9. [Java Native Interface Specification: Design Overview, Oracle](https://docs.oracle.com/en/java/javase/25/docs/specs/jni/design.html)
10. [Compatibility, Using the GNU Compiler Collection (GCC)](https://gcc.gnu.org/onlinedocs/gcc/Compatibility.html)
11. [C++ Name Mangling, RISC-V Ratified Specifications Library](https://docs.riscv.org/reference/abi/riscv-elf-cpp-name-mangling.html)
12. [C++ Dialect Options, Using the GNU Compiler Collection (GCC)](https://gcc.gnu.org/onlinedocs/gcc-15.1.0/gcc/C_002b_002b-Dialect-Options.html)
13. [Interoperability with C, The GNU Fortran Compiler](https://gcc.gnu.org/onlinedocs/gfortran/Interoperability-with-C.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Compilers, interpreters, and toolchains

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/name-mangling

English original: https://www.edgepedia.org/name-mangling
