简体   繁体   English

main()的链接是实现定义的意思是什么?

[英]What does it mean that linkage of main() is implementation defined?

C++ standard section 3.6.1/3 says that C ++标准第3.6.1 / 3节说

The linkage of main is implementation-defined main的链接是实现定义的

What does it mean? 这是什么意思? Why it is implementation defined? 为什么要定义实现? Is it same in C also? C也一样吗?

Because references to the function main are forbidden (it helps if you quote the entire rule), linkage of main has absolutely no effect on user code: 因为禁止引用函数main (如果引用整个规则会有帮助), main链接对用户代码完全没有影响:

The function main shall not be used within a program . 函数main不得在程序中使用 The linkage of main is implementation-defined. main的链接是实现定义的。 A program that defines main as deleted or that declares main to be inline , static , or constexpr is ill-formed. 限定的程序main为已删除或声明maininlinestatic ,或constexpr是形成不良的。 The name main is not otherwise reserved. 名称main不以其他方式保留。

Linkage controls the scope across which the name is usable, the name of the main() function isn't usable by your code anywhere at all, so trying to label it with a linkage doesn't make sense. 链接控制名称可用的范围, main()函数的名称在任何地方都不能被代码使用,因此尝试使用链接标记它是没有意义的。

The purpose of C++ is to provide a portable abstraction over programming. C ++的目的是提供一种可编程的可移植抽象。 Many things are specified by the standard so as to be unambiguous regardless of whether you translate your C++ to assembly, JavaScript, cheese, frying pans or supermodels. 标准规定了许多内容,无论您是将C ++转换为汇编,JavaScript,奶酪,煎锅还是超级模型,都要明确无误。

The linkage of main is not one of those things, because it is a bit of an abstraction leak: it is (theoretically) the function that interacts with the pieces of the executing machine/cheese/frying pan and handles data crossing that boundary. main的链接不是其中之一,因为它有点抽象泄漏:它(理论上)是与执行机器/奶酪/煎锅的各个部分相互作用并处理跨越该边界的数据的函数。 Data in, data out. 数据输入,数据输出。

Substantial details about the main function should not be standard-mandated because the entire purpose of main is to interface with things that the standard cannot control. 关于main功能的实质细节不应该是标准规定的,因为main的整个目的是与标准无法控制的事物接口。

That being said, there are still significant restrictions emplaced upon main , and in most implementations it's not even used as the entrypoint — some internal function in your compiler's C++ runtime will usually act as the the entrypoint, performing static initialisation and a few other things before invoking main , because, well, that's about the only sane way to do it. 话虽如此, 仍然存在对main限制 ,并且在大多数实现中它甚至不用作入口点 - 编译器的C ++运行时中的某些内部函数通常充当入口点,执行静态初始化以及其他一些事情之前调用main ,因为,这是唯一理智的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM