It is (usually) false that execution begins in main but the textbooks aren't "wrong"; it's an abstraction, they're hiding implementation details that you don't need to know. For all intents and purposes, execution begins in main. A C++ implementation can do whatever it wants and still be conformant to the standard, so long as the programmer can't tell the difference between what the standard specifies and what's really happening.
The GNU toolchain actually inserts an _start routine which sets up some registers, allocates a stack and then calls a function called _init, followed by __libc_start_main which calls main, and then when main exits a function called _fini is called.
You can actually disable the _start routine on gcc with the -nostartfiles option, and then you can do this:
1 2 3 4 5 6
|
#include <stdio.h>
void _start(void)
{
printf("hello, world\n");
}
|
It will work, but it will cause a segmentation fault when it tries to return using a non-existent stack. You can also tell ld to use a different entry point than _start so that you can have a program that really does start at main, although I wouldn't recommend it because then you won't have a stack or any constructors or destructors for your global variables unless you do that yourself.
You can verify that main isn't called when you define _start for yourself and compile with -nostartfiles:
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <stdio.h>
void _start(void)
{
return;
}
int main(void)
{
printf("hello, world\n");
return 0;
}
|
This code will also segfault and will not print anything.
And here's a something for fun:
http://codepad.org/WLn9pBYZ