Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

c++ - Compiling with int main(void) fails; main(int argc, char *argv[]) succeeds. Why?

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'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...