3

As Microsoft themselves explain it, console programs use main(), but non-console Win32 programs use WinMain() as the entry-point. In fact, using main() in a Win32 project in Visual Studio will result in a linker error.

But in Qt projects, whether created from Qt Creator or Visual Studio, the GUI programs use main() just like the console programs. How do the Qt folks manage to do it?

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • 1
    You can read more about the difference here: http://stackoverflow.com/questions/13871617/winmain-and-main-in-c-extended . – vahancho Nov 28 '14 at 10:53
  • Its just a matter of addressing in binary executable. A main() function will always work if compiled to the right address. – Sebastian Lange Nov 28 '14 at 10:54

2 Answers2

6

Qt makes use of WinMain() defined in qtbase/src/winmain/qtmain_win.cpp, which subsequently calls our "fictional" int main(int argc, char *argv[]).

P.S. You could figure out this kind of tricks by setting a breakpoint in the debugger, and looking at call stack. In Visual Studio: Menu "Debug" -> Windows -> Call Stack.

Ivan Aksamentov - Drop
  • 12,860
  • 3
  • 34
  • 61
  • Actually, I remember often seeing functions below main/WinMain(), I just never paid any attention to them. So they define WinMain() in Qt itself, right? It would make sense, if they use the CRT, because the CRT would go first, and it would expect WinMain(). – sashoalm Nov 28 '14 at 10:58
1

The problem is in the libraries you link in. Neither main nor WinMain is the entry point for Windows. The real entry point is inside the CRT library. That real entry point calls your main or WinMain. This is necessary for some pre-main CRT initialization

If you have the Qt library, it has a similar need for early initialization and performs similar tricks. You now have both the CRT and Qt libraries, but I think the CRT still goes first, then Qt and finally your main.

MSalters
  • 173,980
  • 10
  • 155
  • 350