Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell cmake not to create a console window?

Tags:

cmake

I can achieve this by gcc :

gcc -mwindows -o simple simple.c

But only find this in cmake:

add_executable(simple WIN32 simple.c)

But it's not exactly the same as -mwindows,

this will require the entry point to be WinMain,

while gcc -mwindows doesn't require this(can be main).

How should I do it properly?

like image 482
user198729 Avatar asked May 02 '10 14:05

user198729


3 Answers

If you use:

add_executable(simple WIN32 simple.c)

then you must provide a WinMain function. That's what the WIN32 flag to add_executable means: it means you're going to make it a Windows program, and provide a WinMain function.

I would recommend doing it this way if you're really writing a Windows application. It's what makes the most sense and fits most naturally with the underlying OS.

However, if you still want to pass gcc the "-mwindows" flag, but use a "main" anyway, then simply add "-mwindows" to the CMAKE_CXX_FLAGS and/or CMAKE_C_FLAGS value. You can do this in the cmake-gui program by adjusting those variables interactively to include "-mwindows" or you can do it with command line CMake, like this:

cmake -DCMAKE_C_FLAGS="-mwindows"
like image 80
DLRdave Avatar answered Oct 24 '22 09:10

DLRdave


As DLRdave has said saying that the executable will be a win32 one means it will have WinMain as the entry point and be a windows application.

If the application is to be cross platform still then the usual means to suppress the console window but still allow use of main is to write a stub WinMain 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 its result.

This prevents the application from having a console window but reduces the disruption to the code of having to use WinMain as the entry point.

like image 45
OmniMancer Avatar answered Oct 24 '22 10:10

OmniMancer


You can add target link option (for new versions of Cmake)

target_link_options(simple PRIVATE -mwindows)

https://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/i386-and-x86_002d64-Windows-Options.html

like image 26
YuryChu Avatar answered Oct 24 '22 09:10

YuryChu