1

Possible Duplicate:
Is main() overloaded in C++?

here's my code:

#include <iostream>

int main(void* a, void* b)
{
    std::cout << "hello standalone " << std::endl;                      
    return 0;
}

different parameters should have a different symbol name after name mangling(void* a, void* b) should be different from (int, char**), but this program doesn't have any problem when running.

Why is that?

Community
  • 1
  • 1
new_perl
  • 7,345
  • 11
  • 42
  • 72

2 Answers2

5

Because main is a special case, and the compiler generates special code for it. Typically, main will be called from a startup routine—often called crt0 in older compilers—written in C, so the compiler will generate main as if it were declared extern "C". But that's in no way required; it's a just a typical implementation.

John Marshall
  • 6,815
  • 1
  • 28
  • 38
James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • Do you have any reference to prove it's a specia case? – new_perl May 23 '12 at 08:02
  • Interesting [crt0](http://en.wikipedia.org/wiki/Crt0) has a wikipedia article. – Jesse Good May 23 '12 at 08:04
  • Typical `nm` output: `000000000040dca9 T main`; which confirms it is not mangled at all. – Matthieu M. May 23 '12 at 08:05
  • @new_perl §3.6.1 in the standard. The description of `main` is pretty clear, and it describes something that is _not_ a normal C++ function. – James Kanze May 23 '12 at 08:10
  • 1
    @JesseGood It's a very poor article, since it suggests something universal, although the very name indicates that it has its origins in C. In fact, `crt0.o` was the name in early Unix of an object file which specified a start address for the executable, and called a C function `main`. When C++ came along, it "mangled" `main` as if it were a C function, so that `crt0.o` could call it. And some of the early implementations inserted extra code at the start of `main` to call constructors. – James Kanze May 23 '12 at 08:14
  • @MatthieuM. And under Windows, `dumpbin` displays `_main`, which is the mangling C uses under Windows (and under most Unix---just not Linux). – James Kanze May 23 '12 at 08:16
3

It depends on the compiler. The standard required signatures for main are:

int main()
int main(int argc, char** argv)
int main(int argc, char* argv[])

But besides these, the compiler is free to provide other signatures as well.

For example, gcc 4.3.4 rejects your code - http://ideone.com/XZp2h

MSVS complains about unresolved externals.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625