Pregunta

I was trying to build an executable using CMake, Qt and Visual Studio that doesn't show the console window.

I found this post and this answer

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /ENTRY:mainCRTStartup")

But I was wondering how QtCreator is able to build an executable that doesn't show the console window without this /ENTRY flag?

¿Fue útil?

Solución

To avoid a console window in a Qt project, which uses CMake and Visual Studio, four entries in the CMakeLists.txt are necessary:

  1. SET(QT_USE_QMAIN true)
  2. INCLUDE(${QT_USE_FILE})
  3. ${QT_LIBARIES}
  4. Add WIN32 to ADD_EXECUTABLE

ADD_EXECUTABLE look then like this:

     ADD_EXECUTABLE(YourProject WIN32
           ...stuff...
     )

For Visual Studio all four steps are necessary. For MinGW step 4 seems to be sufficient. Step 1 must come before step 2.

What do those steps do?

QT_USE_QMAIN is defined in include/QtGui/qwindowdefs.h in the Qt sources. Surprisingly it does nothing else, but:

#if defined(QT_NEEDS_QMAIN)
#define main qMain
#endif

With this Qt defines its own entry point. Of course, this needs qMain to be defined somewhere. Therefore it is necessary to include an extra library, which is called QtMain.lib.

Step 2 is the usual CMake way to find libraries. In this case it includes: path/cmake-2.8/Modules/UseQt4.cmake (Qt4).

Step 3 actually links the found QtMain.lib.

Step 4 causes Windows to use the /subsystem:windows instead of /subsystem:console

The nice thing about this, is that step 1-3 might not be necessary under MinGW, but don't hurt either. So there is no need to distinguish between Visual Studio and MinGW. However, I tested only with Qt4. It might be different for Qt5.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top