12

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?

user198729
  • 58,910
  • 106
  • 245
  • 345
  • This is a duplicate of http://stackoverflow.com/questions/2752792/whats-the-equivalent-of-gccs-mwindows-option-in-cmake. – MKroehnert May 16 '10 at 08:59

3 Answers3

22

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"
DLRdave
  • 13,214
  • 4
  • 49
  • 68
  • Just found this and it solved a very annoying problem of cmake plus a console-less windows app. In particular the desire to remove the console was made well into the project and well after the cmake magic had been weaved. THANK YOU! – Darakian Oct 23 '17 at 15:06
1

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.

OmniMancer
  • 41
  • 2
1

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

YuryChu
  • 171
  • 1
  • 2
  • 13