问题
C++ standard section 3.6.1/3 says that
The linkage of main is implementation-defined
What does it mean? Why it is implementation defined? Is it same in C also?
回答1:
The purpose of C++ is to provide a portable abstraction over programming. 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.
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. 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.
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.
回答2:
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:
The function main shall not be used within a program. The linkage of
main
is implementation-defined. A program that definesmain
as deleted or that declaresmain
to beinline
,static
, orconstexpr
is ill-formed. The namemain
is not otherwise reserved.
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.
来源:https://stackoverflow.com/questions/31222601/what-does-it-mean-that-linkage-of-main-is-implementation-defined