I was wondering if its possible to define main()
inside a class, something like:
struct runtime_entry_point
{
friend int main()
{
}
};
I have tested that and it doesn't work (Almost in GCC 4.8.2):
g++ -o dist/Release/GNU-Linux-x86/turbo build/Release/GNU-Linux-x86/main.o /usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../lib/crt1.o: In function `_start': collect2: error: ld exited with status 1
This sounds to me like a no definition of main()
error.
Later, I have written main in the classic way:
struct runtime_entry_point
{
friend int main()
{
}
};
int main(){}
Now the compilation fails because int main()
was already defined inside the struct runtime_entry_point
! Whats happening here?
Аccording to the article on cppreference.com, the following construction:
struct runtime_entry_point
{
friend int main()
{
}
};
Defines a non-member function, and makes it a friend of this class at the same time. Such non-member function is always inline.
Linker couldn't find main() in object file (inline function), and you can't declare another main() in the same translation unit (as it already declared).