为什么C ++名称为Mangle标识符? [英] Why Does C++ Name-Mangle Identifiers?

查看:217
本文介绍了为什么C ++名称为Mangle标识符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这有必要?由

程序员提供的标识符是唯一的,这就是重要的 - 为什么

将用户指定的内容映射到其他内容?

-

Randy Yates

索尼爱立信移动通信

美国北卡罗来纳州三角研究园
ra ********* @ sonyericsson.com ,919-472-1124

解决方案

Randy Yates写道:

为什么这有必要?由程序员提供的标识符是唯一的,这是重要的 - 为什么
将用户指定的内容映射到其他内容?




void X();

void X(int);


哪个链接符号应为X?




" Randy Yates" < RA ********* @ sonyericsson.com>在消息中写道

news:xx ************* @usrts005.corpusers.net ...

为什么这有必要?由程序员提供的标识符是唯一的,这是重要的 - 为什么
将用户指定的内容映射到其他内容?




但他们不是。


int my_overloaded_function(int x)

{

返回x;

}


int my_overloaded_function(int x,int y)

{

返回x + y;

}


在C ++中定义两个具有相同名称的不同函数是合法的。

通常C ++实现会破坏名称因此非C ++感知链接器可以区分这些函数。


john


Randy Yates写道:


为什么这有必要?由程序员提供的标识符是唯一的,这是重要的 - 为什么
将用户指定的内容映射到其他内容?




因为大多数C ++编译器是围绕依赖于系统的
链接器构建的。那些链接器有一个简单的规则:如果2个函数具有相同名称的
,那么它们是相同的函数。在大多数(如果不是全部)编程语言中,

是真的,这需要链接。


但是在C ++中它是完美的如果只有他们的参数类型不同,则有两个不同的函数

具有相同的名称。


这就是为什么引入名称错误的原因:To将信息添加到

函数的名称,为函数创建不同的名称

,名称相同但参数类型不同。


另一种方法是编写一个新的目标文件

格式和一个可以处理它的新链接器。只要你只有C ++这个就没有大问题了。但正如所说:在大多数大机器(例如大型机)上,只有一个对象

文件格式定义,只有一个系统范围的链接器可以链接

一个应用程序。这是一件好事,因为这意味着你可以自由地链接用不同编程语言编写的模块。

但它也意味着:你必须为<引入一些机制

编译器使函数名称唯一,即使它们在源代码中具有相同的

名称。


-

Karl Heinz Buchegger
kb******@gascad.at


Why is this necessary? The identifiers provide by the
programmer are unique, and that is what is important - why
map what the user has specified to something else?
--
Randy Yates
Sony Ericsson Mobile Communications
Research Triangle Park, NC, USA
ra*********@sonyericsson.com, 919-472-1124

解决方案

Randy Yates wrote:

Why is this necessary? The identifiers provide by the
programmer are unique, and that is what is important - why
map what the user has specified to something else?



void X();
void X( int );

Which linker symbol should be X ?



"Randy Yates" <ra*********@sonyericsson.com> wrote in message
news:xx*************@usrts005.corpusers.net...

Why is this necessary? The identifiers provide by the
programmer are unique, and that is what is important - why
map what the user has specified to something else?



But they aren''t.

int my_overloaded_function(int x)
{
return x;
}

int my_overloaded_function(int x, int y)
{
return x + y;
}

In C++ it''s legal to define two different functions with the same name.
Typically C++ implementations mangle the names so non-C++ aware linkers can
distinguish the functions.

john


Randy Yates wrote:


Why is this necessary? The identifiers provide by the
programmer are unique, and that is what is important - why
map what the user has specified to something else?



Because most C++ compilers are built around a system dependent
linker. Those linker have a simple rule: if 2 functions have
the same name, then they are the same function. This was
true in most (if not all) programming languages which
required linking.

But in C++ it is perfectly valid to have 2 different functions
with the same name, if only their argument types are different.

That''s why name mangling was introduced: To add information to
the name of a function to create different names for functions
with the same name but different argument types.

The alternative would have been to write new a new object file
format and a new linker that can deal with that. As long as you
only have C++ this would be no big problem. But as said: on most
''big machines'' (such as mainframes), there is only one object
file format defined and only one system wide linker to link
an application. That''s a good thing, because it means you can
freely link modules written in different programming languages.
But it also means: You have to introduce some mechanism for
the compiler to make function names unique even if they have identical
names in the source code.

--
Karl Heinz Buchegger
kb******@gascad.at


这篇关于为什么C ++名称为Mangle标识符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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