How do I tell cmake not to create a console window

2020-08-16 03:23发布

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?

标签: cmake
3条回答
forever°为你锁心
2楼-- · 2020-08-16 03:55

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

查看更多
可以哭但决不认输i
3楼-- · 2020-08-16 03:56

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"
查看更多
虎瘦雄心在
4楼-- · 2020-08-16 04:02

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.

查看更多
登录 后发表回答