How QtCreator is able to avoid the console window when building a Windows application?

McLeary

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?

Greenflow

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to open a window in console application?

From Dev

"Console Application Window" on Windows CE Platform

From Dev

QTCreator Not Building (Windows x64, MinGW)

From Dev

How to open WPF window from Console Application

From Dev

qtcreator ignores cmake options when building with cmake

From Dev

How can I determine when my application's console window gets or loses focus?

From Dev

How can I determine when my application's console window gets or loses focus?

From Dev

How to be dynamically either console application or Windows Application

From Dev

How to hide console window of a Go program on Windows

From Dev

C++ Windows: How to close a console window?

From Dev

How to prevent tkinter window from closing when I close Windows command console

From Dev

Opening Windows console application in new window using shortcut key

From Java

How to avoid reinstalling packages when building Docker image for Python projects?

From Dev

How do I avoid nested loops when building a nav?

From Dev

How to make a div scroll able when window height is reached

From Dev

How to embed a Console in a Windows Form application?

From Dev

How to check if the context is a Windows Service or a Console Application

From Dev

How to embed a Console in a Windows Form application?

From Dev

IndexNotReadyException when building the application

From Dev

BufferOverflowException when building application

From Dev

BufferOverflowException when building application

From Dev

How to register a windows service but avoid it being listed in the services console?

From Dev

How to maximise a Windows 10 Universal Application's window to full screen when running on a desktop?

From Dev

How do I set "all windows" by default when left clicking on a application that has more than one window?

From Dev

Avoid application activation and focus in when clicking buttons on it - Windows API or Qt

From Dev

How to dynamically create controls in a dynamically created form window in console application

From Dev

How can I open a console application with a given window size?

From Dev

How to dynamically create controls in a dynamically created form window in console application

From Dev

How to change window title of console application Visual Studio

Related Related

  1. 1

    How to open a window in console application?

  2. 2

    "Console Application Window" on Windows CE Platform

  3. 3

    QTCreator Not Building (Windows x64, MinGW)

  4. 4

    How to open WPF window from Console Application

  5. 5

    qtcreator ignores cmake options when building with cmake

  6. 6

    How can I determine when my application's console window gets or loses focus?

  7. 7

    How can I determine when my application's console window gets or loses focus?

  8. 8

    How to be dynamically either console application or Windows Application

  9. 9

    How to hide console window of a Go program on Windows

  10. 10

    C++ Windows: How to close a console window?

  11. 11

    How to prevent tkinter window from closing when I close Windows command console

  12. 12

    Opening Windows console application in new window using shortcut key

  13. 13

    How to avoid reinstalling packages when building Docker image for Python projects?

  14. 14

    How do I avoid nested loops when building a nav?

  15. 15

    How to make a div scroll able when window height is reached

  16. 16

    How to embed a Console in a Windows Form application?

  17. 17

    How to check if the context is a Windows Service or a Console Application

  18. 18

    How to embed a Console in a Windows Form application?

  19. 19

    IndexNotReadyException when building the application

  20. 20

    BufferOverflowException when building application

  21. 21

    BufferOverflowException when building application

  22. 22

    How to register a windows service but avoid it being listed in the services console?

  23. 23

    How to maximise a Windows 10 Universal Application's window to full screen when running on a desktop?

  24. 24

    How do I set "all windows" by default when left clicking on a application that has more than one window?

  25. 25

    Avoid application activation and focus in when clicking buttons on it - Windows API or Qt

  26. 26

    How to dynamically create controls in a dynamically created form window in console application

  27. 27

    How can I open a console application with a given window size?

  28. 28

    How to dynamically create controls in a dynamically created form window in console application

  29. 29

    How to change window title of console application Visual Studio

HotTag

Archive