问题描述
我正在尝试学习和理解 C++ 中的名称修饰.以下是一些问题:
I am trying to learn and understand name mangling in C++. Here are some questions:
(1) 来自 devx
当全局函数重载时,每个重载版本生成的重整名称都是唯一的.名称修饰也适用于变量.因此,具有相同用户给定名称的局部变量和全局变量仍然获得不同的重整名称.
When a global function is overloaded, the generated mangled name for each overloaded version is unique. Name mangling is also applied to variables. Thus, a local variable and a global variable with the same user-given name still get distinct mangled names.
除了重载函数和同名全局和局部变量之外,还有其他使用名称修改的例子吗?
Are there other examples that are using name mangling, besides overloading functions and same-name global and local variables ?
(2) 来自 维基
如果语言允许使用相同标识符命名不同实体,只要它们占据不同的命名空间(其中命名空间通常由模块、类或显式命名空间指令定义),就会出现这种需求.
The need arises where the language allows different entities to be named with the same identifier as long as they occupy a different namespace (where a namespace is typically defined by a module, class, or explicit namespace directive).
我不太明白为什么name mangling只适用于标识符属于不同命名空间的情况,因为重载函数可以在同一个命名空间,同名的全局和局部变量也可以在同一个空间.如何理解?
I don't quite understand why name mangling is only applied to the cases when the identifiers belong to different namespaces, since overloading functions can be in the same namespace and same-name global and local variables can also be in the same space. How to understand this?
同名但作用域不同的变量是否也使用名称修饰?
Do variables with same name but in different scopes also use name mangling?
(3) C 有名称修改吗?如果不是,如何处理一些全局变量和局部变量同名的情况?C没有重载函数吧?
(3) Does C have name mangling? If it does not, how can it deal with the case when some global and local variables have the same name? C does not have overloading functions, right?
谢谢和问候!
推荐答案
C 不进行名称修改,尽管它确实在函数名称前加了一个下划线,所以 printf(3)
是实际上 _printf
在 libc 对象中.
C does not do name mangling, though it does pre-pend an underscore to function names, so the printf(3)
is actually _printf
in the libc object.
在 C++ 中,情况有所不同.它的历史是最初 Stroustrup 创建了C with classes"或 cfront,一个编译器可以将早期的 C++ 翻译成 C.然后剩下的工具——C 编译器和链接器我们将用来生成目标代码.这意味着必须以某种方式将 C++ 名称转换为 C 名称.这正是 名称修改 所做的.它为每个类成员和全局/命名空间函数和变量提供唯一的名称,因此命名空间和类名称(用于解析)和参数类型(用于重载)以某种方式包含在最终链接器名称中.
In C++ the story is different. The history of it is that originally Stroustrup created "C with classes" or cfront, a compiler that would translate early C++ to C. Then rest of the tools - C compiler and linker would we used to produce object code. This implied that C++ names had to be translated to C names somehow. This is exactly what name mangling does. It provides a unique name for each class member and global/namespace function and variable, so namespace and class names (for resolution) and argument types (for overloading) are somehow included in the final linker names.
使用 nm(1)
等工具很容易看到这一点 - 编译您的 C++ 源代码并查看生成的符号.以下是在带有 GCC 的 OSX 上:
This is very easy to see with tools like nm(1)
- compile your C++ source and look at the generated symbols. The following is on OSX with GCC:
namespace zoom
{
void boom( const std::string& s )
{
throw std::runtime_error( s );
}
}
~$ nm a.out | grep boom
0000000100001873 T __ZN4zoom4boomERKSs
在 C 和 C++ 中,局部(自动)变量不产生符号,而是存在于寄存器或堆栈中.
In both C and C++ local (automatic) variables produce no symbols, but live in registers or on stack.
局部变量在生成的目标文件中没有名称,仅仅是因为链接器不需要知道它们.所以没有名字,没有破坏.其他所有内容(链接器必须查看的内容)在 C++ 中都是名称错误的.
Local variables do not have names in resulting object file for mere reason that linker does not need to know about them. So no name, no mangling. Everything else (that linker has to look at) is name-mangled in C++.
这篇关于关于 C++ 中名称修改的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!