Are you getting error like this?
Undefined symbols:
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
This is not a compiler error, but a linker error.
In compilation, each source file is translated to an object file.
There is no check whether int main()
exists because the program may consist of multiple sources, and the main()
is only defined in one of these, or it doesn't even need to exist (e.g. in a dynamic library). Since the source
short main[] = {};
is considered a valid declaration (create a global short
array named main
and initialize to an empty array) by the compiler, it will not generate any errors.
The detection of whether int main()
exists is checked by the linker. The linker binds the compiled object files to a working executable. If the linker cannot find the symbol main
, it will complain like the one I described above. Unfortunately, the conventional C ABI does not differentiate between functions or or kinds of exported variables. So even if the main
is declared as an array, since the linker only knows "something called main
exists" and cannot check more, it will pass as well.
The result is that the program is generated without error although it is wrongly written.
When the program is run, the so-called main
does not hold executable code. Rather, it is just some data (likely zeroed). So the system may do anything unexpected (in your case, it is a SEGFAULT).
This can actually be caught when compiling with the -Wall
flag in gcc, which gives a warning:
<stdin>:1: warning: ‘main’ is usually a function