Skip to content

Instantly share code, notes, and snippets.

@hnOsmium0001
Last active July 27, 2021 18:27
Show Gist options
  • Save hnOsmium0001/1fa4fc3e0a15542f9cbaeddf8167a188 to your computer and use it in GitHub Desktop.
Save hnOsmium0001/1fa4fc3e0a15542f9cbaeddf8167a188 to your computer and use it in GitHub Desktop.
CMake snippet to add to CMakeLists.txt to make some target use `int main()` while building to the GUI ("windows") subsystem.
# No console window when targetting windows
if(WIN32)
function(handle_gnu_style_compiler)
# Supposedly the flag -mwindows would automatically make the executable use GUI subsystem
# But, when subsystem is set to GUI, linker will only search WinMain and wWinMain but not the standard main (it seems like)
# so creating GUI executable fails and the linker silently reverts to the default, CUI subsystem
target_link_options(${TARGET_NAME} PRIVATE -Wl,-subsystem:windows)
target_link_options(${TARGET_NAME} PRIVATE -Wl,-entry:mainCRTStartup)
endfunction()
function(handle_msvc_style_compiler)
target_link_options(${TARGET_NAME} PRIVATE /SUBSYSTEM:windows /ENTRY:mainCRTStartup)
endfunction()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# GCC (MinGW)
handle_gnu_style_compiler()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
if(CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
# MSVC-style argument clang (clang-cl.exe)
handle_msvc_style_compiler()
elseif(CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "GNU")
# GNU-style argument clang (clang.exe and clang++.exe)
handle_gnu_style_compiler()
endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
handle_msvc_style_compiler()
endif()
endif()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment