Being a low-level programmer, I often work with the module startup code for executables, so I understand pretty well how code like "crt0" work. When writing C++ code, I've generally declared main
as extern "C"
to match what the C startup code is going to do to call main
. I thus usually use this declaration for main
(and wmain
if specifically targeting Windows):
extern "C" int main(int argv, const char *const *argv)
extern "C" int __cdecl wmain(int argv, const wchar_t *const *argv)
Is it legal to use extern "C"
on main
? Also, is const char *const *
legal for argv's type as opposed to char *[]
?
By declaring a function with extern "C" , it changes the linkage requirements so that the C++ compiler does not add the extra mangling information to the symbol. This pattern relies on the presence of the __cplusplus definition when using the C++ compiler. If you are using the C compiler, extern "C" is not used.
You need to use extern "C" in C++ when declaring a function that was implemented/compiled in C. The use of extern "C" tells the compiler/linker to use the C naming and calling conventions, instead of the C++ name mangling and C++ calling conventions that would be used otherwise.
Using extern "C" lets the compiler know that we want to use C naming and calling conventions. This causes the compiler to sort of entering C mode inside our C++ code. This is needed because C++ compilers mangle the names in their symbol table differently than C compilers and hence behave differently than C compilers.
“extern” keyword is used to extend the visibility of function or variable. By default the functions are visible throughout the program, there is no need to declare or define extern functions. It just increase the redundancy. Variables with “extern” keyword are only declared not defined.
The linkage is implementation defined (3.6.1p3):
The linkage (3.5) of main is implementation-defined.
Also, for your latter question, that is perfectly acceptable to have const char* const*
(3.6.1p2):
An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined.
extern "C"
only tells the C++ compiler specifically to NOT decorate or use name wrangling on generated function labels.
Yes, both are legal.
The standard blesses two forms of main
:
int main()
int main(int argc, char* argv[])
These forms are what any implementation must recognize. Everything else is your implementation being easy with your code and letting you be creative. It is not illegal, as the standard specifically allows it to recognize other forms of main
.
The startup code is normally written in a way that allows it to call main
having no linkage declaration, because that's how the standard says main
should be. The compiler usually treats main
specially as having C linkage, as allowed by the standard, so that's how the startup code declares it. This is of no interest to a normal programmer. He just needs to follow the standard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With