为什么C编译器prePEND强调外部的名字呢? [英] Why do C compilers prepend underscores to external names?

查看:228
本文介绍了为什么C编译器prePEND强调外部的名字呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C已经工作了这么久,通常编译器的事实添加下划线到的extern 开始只是理解......但是,< A HREF =htt​​p://stackoverflow.com/questions/2627004/embedding-binary-blobs-using-gcc-mingw>另一SO质疑今天让我纳闷为什么加下划线的真正原因。一个维基百科的文章称,其中一个原因是:


  

这是C编译器,以prePEND领先强调所有外部范围的程序标识符,以避免与运行时语言支持贡献冲突常见的做法


我觉得有至少道理的的内核,也似乎没有真正回答这个问题,因为如果下划线被添加到所有实习医生将助益不大preventing冲突。

有没有人有理前导下划线良好的信息?

时的那Unix的科瑞()系统调用不以'E'结束的原因加下划线的部分?我听说在一些平台上早期的接头有6个字符的名称的限制。如果是这样的话,那么prepending下划线外部名称似乎是一个彻头彻尾的疯狂的想法(我现在只有5个字符用...玩)。


解决方案

  

这是C编译器,以prePEND领先强调所有外部范围的程序标识符,以避免与运行时语言支持贡献冲突常见的做法


如果运行时支持是由编译器提供,你会觉得它会更有意义prePEND在运行时支持下划线的几个外部标识,而不是!

当C编译器第一次出现,基本替代编程在这些平台上的C用汇编语言编程了,这是(有时仍然是)链接起来反对用汇编语言编写,并C.所以,真正有用的文件(恕我直言)前导下划线添加到外部C标识符是为了避免与您自己的组装code标识符冲突。

(另见 GCC的 ASM 标签延伸;并注意此prepended下划线可以考虑的名称重整的像C更复杂的语言使用++更复杂的名字改编一个简单的形式,但这是。在那里开始了。)

I've been working in C for so long that the fact that compilers typically add an underscore to the start of an extern is just understood... However, another SO question today got me wondering about the real reason why the underscore is added. A wikipedia article claims that a reason is:

It was common practice for C compilers to prepend a leading underscore to all external scope program identifiers to avert clashes with contributions from runtime language support

I think there's at least a kernel of truth to this, but also it seems to no really answer the question, since if the underscore is added to all externs it won't help much with preventing clashes.

Does anyone have good information on the rationale for the leading underscore?

Is the added underscore part of the reason that the Unix creat() system call doesn't end with an 'e'? I've heard that early linkers on some platforms had a limit of 6 characters for names. If that's the case, then prepending an underscore to external names would seem to be a downright crazy idea (now I only have 5 characters to play with...).

解决方案

It was common practice for C compilers to prepend a leading underscore to all external scope program identifiers to avert clashes with contributions from runtime language support

If the runtime support is provided by the compiler, you would think it would make more sense to prepend an underscore to the few external identifiers in the runtime support instead!

When C compilers first appeared, the basic alternative to programming in C on those platforms was programming in assembly language, and it was (and occasionally still is) useful to link together object files written in assembler and C. So really (IMHO) the leading underscore added to external C identifiers was to avoid clashes with the identifiers in your own assembly code.

(See also GCC's asm label extension; and note that this prepended underscore can be considered a simple form of name mangling. More complicated languages like C++ use more complicated name mangling, but this is where it started.)

这篇关于为什么C编译器prePEND强调外部的名字呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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