Search code examples
c++functionlinkage

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


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?


Solution

  • 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.