6

Problem

Why would compiling a program which has an int main(void) main function differ from compiling a program which has an int main(int argc, char *argv[]) main function, if the program does not use arguments passed on the command line?

Is this OS or compiler specific? I do not get the same results using mingw and g++(which is weird isn't it as wingw is a port of gcc).


Example

Code

#include <iostream> #include"SDL/SDL.h" int main(void) { return 0; } 

Compilation commands

g++ test.cpp; #g++ 4.4.5 i586-mingw32msvc-g++ test.cpp; # mingw 4.4.4 

Error

(Given by the second command.)

a(main.o):(.text+0x85): undefined reference to `_WinMain@16' 
10
  • 1
    It's compiler specific ... but int main (int argc, char *argv[]) is "correct". I would recommend ALWAYS using the latter form in ALL your C and C++ code. Commented Dec 29, 2011 at 21:01
  • @paulsm4: int main(void) is also correct. Commented Dec 29, 2011 at 21:01
  • stackoverflow.com/questions/3156423/… Commented Dec 29, 2011 at 21:03
  • 1
    Please provide an example program, the exact command you use to invoke the compiler, the exact output of the compiler, and the kind and version of the compiler. Without this information, no one can definitively answer your question. Commented Dec 29, 2011 at 21:03
  • You might consider telling us something a bit more specific than it "fails". Compile error (if so, what's the message?) Linker error (if so, what's the message?) Error at runtime (if so, what?) As it stands, people can just guess. Commented Dec 29, 2011 at 21:05

2 Answers 2

11

This is SDL thing. On Windows, when you include SDL.h,main is redefined to SDL_main which calls WinMain (the real entry point in non-console Windows apps), does some initialization and finally calls your main code. It has a signature with argc and argv and you're pretty much required to follow it, so int main() won't work.

Sign up to request clarification or add additional context in comments.

5 Comments

(I edited incorrectly -> re-edited) Why would it work with g++ then?
@danjjl Because it's only redefined on Windows. (You're compiling with g++ on some other platform, I assume)
Nice :), where did you find the information? is this still true in my example that does not really use SDL?
It's mentioned in FAQ section on SDL's website and there are numerous threads about this issue in their forums.
Right it was because I was not linking SDL (event do I would have thought I did not need to as it was not being used). Linking SDL gives Undefined reference to 'SDL_main' which is covered in the FAQ
-1

The specification of main(...) is a contract. In the C language, the contract says that the arguments are int and char **. This is a requirement your program has to fulfill, if it wants the environment to interact with it.

Whether or not your program wants to use the parameters is a different issue -- it just has to abide by the contract that there is a functiona named main, with the correct order and type of parameters.

1 Comment

This question is about C++, not C. That said, this answer is incorrect regardless: int main(void) is a perfectly valid declaration of main in both C (C90, C99, and C11) and C++ (C++98, C++03, and C++11).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.