Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it legal C++ to declare main as extern "C"?

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 *[]?

like image 543
Myria Avatar asked Nov 06 '13 03:11

Myria


People also ask

Can you use extern C in C?

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.

Should you use extern C?

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.

Why do you need extern C?

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.

Are C functions extern by default?

“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.


3 Answers

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.

like image 81
Jesse Good Avatar answered Oct 05 '22 23:10

Jesse Good


extern "C" only tells the C++ compiler specifically to NOT decorate or use name wrangling on generated function labels.

Yes, both are legal.

like image 39
Aniket Inge Avatar answered Oct 06 '22 00:10

Aniket Inge


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.

like image 41
n. 1.8e9-where's-my-share m. Avatar answered Oct 06 '22 00:10

n. 1.8e9-where's-my-share m.