Reputation: 41
From the books i have read about C and C++ I understood that the entry point in C program must be main.Till now i had made only console applications and now im starting to learn about windows applications. So my questions is :
Why the entry point in Win32 project is not main (but WinMain) and how is it possible to be different ( maybe main calls WinMain ?) ?
ps. Sorry for bad english
Upvotes: 3
Views: 1335
Reputation: 3911
In fact main
is just a name look at assembly language: you are free to declare your Entry Point
as you want eg:
.code
START:
ret
END START
But in C++ it doesn't allow you to define your own EP. So must implement the convention: for console you need main, wmain, win32: WinMain, for Dll: dllmain...
Upvotes: 1
Reputation: 385144
It is true that C++ requires main
to be the "entrypoint" of the program, at least under what's called "hosted implementations" (the ones you're probably using). This is why you get a linker error with GCC if you forgot to define a main
.
However, in practice, the gap where your program ends and the "runtime" begins, makes things seem a little more wooly — when your program is launched, the first functions called are actually inside the runtime, which will eventually get around to invoking main
. It is actually this invocation which causes the linker error if you forgot to define a main
.
Microsoft has decided that, for Windows GUI programs, instead of invoking main
, their runtime will invoke WinMain
. As a consequence, you have to define a function WinMain
for it to find, instead of main
. Technically this violates the C++ standard, but it works because Microsoft makes it work.
Upvotes: 4
Reputation: 596011
The actual name and signature of the entry point in your code is dictated by the runtime framework you decide to use inside your EXE.
When the OS executes an EXE, it first calls an entry point specified in the EXE header by the linker, and is usually located in the compiler vendor's runtime library.
The runtime library's entry point initializes the library, sets up globals, etc, and then finally calls an entry point that your code must implement (the runtime library contains a reference to an external entry point, and the linker hooks up your code's entry point to that reference).
So, your code's entry point is whatever the runtime library requires it to be. The standard entry point for console apps in C/C++ is main
, and the traditional entry point for Windows GUI apps is WinMain
. But this is not a requirement as far as the OS is concerned.
Upvotes: 2