I'm making an application in Qt Creator, with cmake and MinGW as compiler. I've seen this question being answered for other people, but they used regular Qt projects with .pro files, while I use a CMakeLists.txt file. So these posts were of no help to me.
The problem is that my application opens a console when booted, and as usual, closing this console will close the application as well. I want to keep the application from opening a console, so that it is more user-friendly for people who don't need any debug information and such.
I had the same issue, but solved it by adding:
If I don't use
LINK_LIBRARIES(${QT_QTMAIN_LIBRARY})
I get error:error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
By default, and in contrast to qmake, cmake builds Qt apps with enabled console window under windows (windows binaries can use different entry points - the console window is one of them).
You can disable the console window appearing via setting the
WIN32_EXECUTABLE
cmake property on the executable.This can be achieved either via setting an
add_executable
option, i.e.or via setting the property explicitly:
Using
set_property()
is helpful when the console window should conditionally be disabled, e.g.:The
WIN32_EXECUTABLE
property has no effect when compiling on platforms other than windows (cf.CMAKE_WIN32_EXECUTABLE
).As with the
WIN32
cmake variable, theWIN32_EXECUTABLE
property also configures the console window when compiling a win64 executable.For building with Mingw, add a CMake command:
Replace
target_name
with your target's name (first parameter to add_executable)This is an old question, but anyways, there is a better solution than all of the other ones posted here:
Adding this will automatically handle everything for you. CMake should actually output a warning if you don't set this policy, at least that's how I learned of its existence.
You will most likely have a line such as the following in your CMakeLists.txt:
where of course the dots are further arguments. Change this to:
to specify that it's a Win32 application and not a console application.
Or, as can be found on the CMAKE website "If
WIN32
is given the propertyWIN32_EXECUTABLE
will be set on the target created." And when WIN32_EXECUTABLE is set it will "Build an executable with a WinMain entry point on windows."