简体   繁体   中英

function main without definition in C and C++

Why this code compile successful in C and will give you an error in C++?

int main;

Is it standard-conforming in a hosted environment? Can you quote the standard?

I've tested it with gcc.

Why this code compile successful in C and will give you an error in C++?

Because of C++ name mangling. Basically, in all practical implementations, the linker looks for a symbol named main (or variants of it, I've seen _main on Apple's platforms) - in C, that can be either the main() function or an extern storage variable named main - the point is that usually C implementations (compilers, toolchains) don't differentiate between variables and functions at the linker level, that's why providing one symbol, be it either a variable or a function, named main() may seem to be enough . In fact, in a hosted environment, as per the Standard, the resulting program (executable) won't be conforming, because there, it is required that the main() function be implemented.

In C++, usually name mangling is used (in order to achieve features of C++ such as function overloading), and that means that the compiler names the resulting symbol in the executable file differently depending on its type, on the fact if it's a function, a variable, a function with a different signature , and other circumstances. So the linker basically won't find the symbol corresponding to the expected int main(int, char *[]) function and will issue an error message.

Is it standard-conforming?

Not defining the main() function isn't (see the first part). As far as I can tell, having a variable named main along with the main function is valid C++, but it is certainly bad practice.

Can you quote the Standard?

Yes please (emphasis mine):

C++ 98, paragraph 3.6.1:

A program shall contain a global function called main() , which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main() function.

C99, paragraph 5.1.2.2.1

5.1.2.2.1 Program startup

1 The function called at program startup is named main. The implementation declares no prototype for this function. I

From ISO/IEC 14882:1998(E) (aka C++98), 3.6.1 Main function :

An implementation shall not predefine the main function. This function shall not be overloaded. It shall 2 have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main: int main() { /* ... */ } and int main(int argc, char* argv[]) { /* ... */ } In the latter form argc shall be the number of arguments passed to the program from the environment in which the program is run. If argc is nonzero these arguments shall be supplied in argv[0] through
argv[argc-1] as pointers to the initial characters of null-terminated multibyte strings (NTMBSs) (17.3.2.1.3.2) and argv[0] shall be the pointer to the initial character of a NTMBS that represents the name used to invoke the program or "". The value of argc shall be nonnegative. The value of argv[argc] shall be 0. [Note: it is recommended that any further (optional) parameters be added after
argv. ]


The function main shall not be used (3.2) within a program. The linkage (3.5) of main is 3 implementation-defined. A program that declares main to be inline or static is ill-formed. The name main is not otherwise reserved. [Example: member functions, classes, and enumerations can be called main, as can entities in other namespaces. ]


int main; does not comply with the above ("All implementations shall allow both of the following definitions of main", "The function main shall not be used (3.2) within a program").

Yes, it's valid.

It declares an integer named main .

I think , I found one . It's not a solution but point to remember If you use

gcc -Wall -Werror <file.c>

You will get warning is treated as errors:

main is usually a function name

So its best to compile with -Wall so that you can see all the warnings as well

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM