I have recently started using CMake, and was trying to build a GUI application, that doesn't have the console window on Windows. So in my CMakeLists.txt
file, I did this:
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
add_executable(${EXECUTABLE_NAME} main.cpp)
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
add_executable(${EXECUTABLE_NAME} WIN32 main.cpp) #WIN32 So the console window does not open on Windows
endif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
With this, the solution worked, and the console window does not open on Windows. However, this comes at a cost. When I try to build the solution, I realize that I have to change the signature of the function to WinMain
, so I changed my main code to the following:
#ifdef _WIN32
#include <Windows.h>
int WINAPI WinMain(HINSTANCE, HINSTANCE, PSTR, int) //Windows signature since creating WIN32 application without console
#else
int main()
#endif
{
// ... GUI code
}
Unfortunately, I absolutely detest this, since it ruins the whole point of using CMake. I don't want to have to change anything in my code that is based on different platforms. This leads me to my question. How do I set the C++ application entry point to main()
on Windows when making a GUI application without having to set it manually in Visual Studio? Can I do this directly in CMake using a cross-platform method? Or will I have to use the #if/#else/#endif
solution? The only improvement to the solution above is using a macro MAIN_FUNCTION
that does the preprocessor conditional. I want to avoid this as well.
On the other hand, is there another way to get rid of the console window in a GUI application on Windows that I didn't know of using CMake without using the WIN32 option?