2

As per ISO_14882_2014's, 3.6.1 Main function [basic.start.main]

The linkage (3.5) of main is implementation-defined.

What is its linkage for g++ as g++'s official document does not seem to have this information.

Ujjwal
  • 151
  • 8

2 Answers2

2

It is not directly mentioned in the manual, but in the chapter on "GCC Command Options" we have this:

-Wmain Warn if the type of main is suspicious. main should be a function with external linkage, returning int, taking either zero arguments, two, or three arguments of appropriate types. This warning is enabled by default in C++ and is enabled by either ‘-Wall’ or ‘-Wpedantic’.

[Emphasis Added]

From this we can infer that the linkage for main in this implementation is external.

P.W
  • 26,289
  • 6
  • 39
  • 76
  • Thanks. The above statement says: `.....taking either zero arguments, two, or three arguments of appropriate types...` But as per the ISO: `An implementation shall allow both — a function of () returning int and — a function of (int, pointer to pointer to char) returning int` how we are taking about 3 arguments here? – Ujjwal Apr 02 '19 at 07:42
  • Three argument `main` is an extension provided by some compilers. See this post: [Main function with three arguments](https://stackoverflow.com/questions/21939379/main-function-with-three-arguments) – P.W Apr 02 '19 at 07:50
0

This is to prevent you from accidentally calling your main function - or writing a 'wrong' main function.

The function main shall not be used within a program. The linkage (3.5) 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.

darune
  • 10,480
  • 2
  • 24
  • 62