2

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?

Destructor
  • 14,123
  • 11
  • 61
  • 126
  • possible duplicate of [Undefined, unspecified and implementation-defined behavior](http://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior) – omerfarukdogan Jul 04 '15 at 16:03
  • 1
    @farukdgn: no. I amn't asking about what is implementation defined behavior. This isn't duplicate. The question is why it is implementation defined? The question you said is different from mine. – Destructor Jul 04 '15 at 16:05
  • 2
    It means that the C++ standard doesn't care how linkage of `main` is implemented, it can differ depending on the compiler. The compiler isn't bound to certain criteria and thus it's implementation isn't either. – Hatted Rooster Jul 04 '15 at 16:05
  • @JameyD: ok. but why? – Destructor Jul 04 '15 at 16:07
  • 2
    Because an application entry point is - by definition - platform specific. – IInspectable Jul 04 '15 at 16:07
  • @IInspectable: How it can be platform specific? Can you give some examples? – Destructor Jul 04 '15 at 16:08
  • The only purpose of an application entry point is to be called by the platform, when a program is executed. It is where the OS meets the language specification. You have to provide some leeway so that they can match up. A standardized linkage wouldn't buy you anything either, as the application entry point is only relevant to the platform running the program anyway. – IInspectable Jul 04 '15 at 16:15
  • Related: http://stackoverflow.com/a/6982101/560648 – Lightness Races in Orbit Jul 04 '15 at 16:20
  • @PravasiMeet: How can it _not_ be? Do you know what `main` is? – Lightness Races in Orbit Jul 04 '15 at 16:20

2 Answers2

6

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 defines main as deleted or that declares main to be inline, static, or constexpr is ill-formed. The name main 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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
2

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.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055