关于 C++ 中名称修饰的问题 [英] questions about name mangling in C++

查看:20
本文介绍了关于 C++ 中名称修饰的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习和理解 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).

我不太明白为什么名称修饰只适用于标识符属于不同命名空间的情况,因为重载函数可以在同一个命名空间中,并且同名全局变量和局部变量也可以在同一个空间中.这个怎么理解?

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 名称.这正是 name mangling 所做的.它为每个类成员和全局/命名空间函数和变量提供一个唯一的名称,因此命名空间和类名(用于解析)和参数类型(用于重载)以某种方式包含在最终的链接器名称中.

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++ 源代码并查看生成的符号.以下是在 OSX 上使用 GCC:

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++ 中名称修饰的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆