How to tell cmake not to create a console window?

I can achieve this with gcc:

gcc -mwindows -o simple simple.c 

But find this only in cmake:

 add_executable(simple WIN32 simple.c) 

But this is not exactly the same as -mwindows ,

this requires the entry point to be WinMain ,

while gcc -mwindows does not require this (maybe main ).

How should I do it right?

+7
cmake
source share
2 answers

If you use:

 add_executable(simple WIN32 simple.c) 

then you must provide the WinMain function. What the WIN32 flag means for add_executable means: you are going to make it a Windows program and provide a WinMain function.

I would recommend doing it this way if you are really writing a Windows application. This is what is most convenient and fits most naturally with the underlying OS.

However, if you still want to pass gcc the -mwindows flag, but use main anyway, then just add -mwindows to CMAKE_CXX_FLAGS and / or CMAKE_C_FLAGS. You can do this in cmake-gui by configuring these variables interactively to include "-mwindows", or you can do this using the CMake command line, for example:

 cmake -DCMAKE_C_FLAGS="-mwindows" 
+12
source share

As DLRdave said, saying that the executable will be win32, it means that it will have WinMain as an entry point and be a Windows application.

If the application should be cross-platform, then the usual way to suppress the console window, but still allow the use of main, is to write a WinMain stub as found in the SDL or SFML libraries, which simply calls the main function with the global variables __argc and __argv as arguments and returns the result.

This prevents the application from launching via the console window, but reduces the violation of WinMain usage code as an entry point.

+1
source share

All Articles