是否有任何类型的C或C ++链接器检查? [英] Is there any type checking in C or C++ linkers?

查看:79
本文介绍了是否有任何类型的C或C ++链接器检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是说错交联剂作任何函数参数的检查。他们不检查函数调用的数量和种类也没有检查全局数据引用的类型。这是适用于所有的接头?

Am I right in saying linkers make no function parameter checks. They do not check the number or types of function calls nor do they check the type of global data references. Is this true for all linkers?

我使用锵在x86-64的目标Linux操作系统。是否连接检查引用是正确的段?或者是在效果只是一个空隙外部基准*至于接头而言

I'm using Clang targeting Linux on x86-64. Does the linker check that references are in the right segment? Or is an external reference in effect just a void * as far as the linker is concerned?

我现在是高级语言的背景C#和Scala的到来,因此这似乎是显而易见的,以那些沉浸在自己水平低的世界。我已经写了几个我在汇编功能(系统调用),我发现没有参数的原型为在汇编外部函数。

I'm coming from a high level language background C# and Scala, so this may seem obvious to those that have immersed themselves in the low level world. I've written a couple of my functions (system calls) in assembler and I noticed there were no parameter prototypes for external functions in the assembler.

上下文:实际上我正在编写一个编译器。就目前而言,我瞄准preprocessedç。我的文件与系统调用汇编函数,但替代品C ++,汇编,甚至要用机器code,所以我试图权衡成本和收益,尤其是类型检查,汇编器/编译器/ 链接我可以用它来检查我自己的程序和它的函数原型生成的正确性。

Context: I'm actually writing a compiler. For the moment I'm targeting preprocessed C .i files with assembler functions for system calls, but the alternatives are C++, assembler or even machine code, so I'm trying to weigh the costs and benefits, particularly the type checking, of the assembler / compiler / linker I can use to check the correctness of my own programme and its function prototype generation.

推荐答案

由于@Yakk解释,功能可以根据自己的参数重载,因此编译器产生错位的函数名,包括参数及其类型的信息。链接器大多只是象征检查名称和大小,但由于重整确保函数的名称是不同的,不匹配的参数不能链接。

As @Yakk explained, functions can be overloaded based on their parameters, and so the compiler generates mangled function names that include information about parameters and their types. The linker mostly just checks symbol names and sizes, but since mangling ensures that functions' names are different, mismatched parameters won't link.

函数的返回类型的并非是混淆的(因​​为超载的返回类型是不合法的),所以如果你声明 INT测试()在一个翻译单元,并在另一个电话浮点测试(),链接器将不能抓住它,你会得到不好的结果。

Function return types aren't part of the mangling (because overloading on return type isn't legal), so if you declare int test() in one translation unit and call float test() in another, the linker won't catch it, and you'll get bad results.

同样,全局变量(和类的静态成员等)的类型不被链接检查,因此,如果你声明的extern INT测试; 中一个转换单元,并定义浮动测试; 在另一个,你会得到不好的结果。

Similarly, the types of global variables (and static members of classes and so on) aren't checked by the linker, so if you declare extern int test; in one translation unit and define float test; in another, you'll get bad results.

链接器可以在某些情况下,有两种不同的翻译单元比较的尺寸的象征,并能赶上这样的一些问题。

The linker can, in some circumstances, compare the size of a symbol in two different translation units and can catch a few problems in this way.

在实践中,这是很少在普通的C ++开发的一个问题,因为无论何时由> 1转换单元需要一个函数或变量或类,你会在随机配备两个翻译单位的头文件声明它,而编译器将捕捉任何错误,链接器运行之前。 (一个实例它可以是一个问题是,如果您使用的是外部的,二进制库,你必须为它不来匹配磁带库的头文件。)

In practice, this is rarely an issue in normal C++ development, because whenever a function or variable or class is needed by >1 translation unit, you'll declare it in a header file that's included in both translation units, and the compiler will catch any errors before the linker even runs. (One instance where it can be an issue is if you're using an external, binary library and the header files you have for it don't match the library.)

这篇关于是否有任何类型的C或C ++链接器检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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