CMake: How to use various ADD_EXECUTABLE to build debugging?

I would like to create my application so that the debug mode is a console application, and the release mode is a Win32 application. According to the documentation, I need to add WIN32 to add_executable depending on whether I want a console application or not.

Since I use Visual Studio, I cannot use CMAKE_BUILD_TYPE (the generated project contains several configurations). How can I tell CMAKE to use WIN32 to build the release and omit it for debug builds?

+4
source share
2 answers

Quote http://www.cmake.org/Wiki/VSConfigSpecificSettings

 if(WIN32) set_target_properties(WindowApplicationExample PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE") set_target_properties(WindowApplicationExample PROPERTIES COMPILE_DEFINITIONS_DEBUG "_CONSOLE") set_target_properties(WindowApplicationExample PROPERTIES LINK_FLAGS_RELWITHDEBINFO "/SUBSYSTEM:CONSOLE") set_target_properties(WindowApplicationExample PROPERTIES COMPILE_DEFINITIONS_RELWITHDEBINFO "_CONSOLE") set_target_properties(WindowApplicationExample PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:windows") set_target_properties(WindowApplicationExample PROPERTIES LINK_FLAGS_MINSIZEREL "/SUBSYSTEM:windows") endif(WIN32) 

UPDATE This feature is broken in recent versions due to a bug . One workaround I found is to specify "/ SUBSYSTEM: windows" instead of "/ SUBSYSTEM: WINDOWS". This seems to work for some reason.

+6
source

I don’t know if this error has been fixed in CMake. I am using VC ++ 2010 express and CMake v2.8.10.1 (which is currently the latest version) and I still have the same problem.

A working solution was provided here : change the source code (e.g. main.cpp / main.c) by adding:

 #ifndef NDEBUG #pragma comment(linker, "/SUBSYSTEM:CONSOLE") #endif 

Alternatively, you can add the linker flag “/ SUBSYSTEM: WINDOWS” to the release mode assembly. I am using this definition, which seems to work:

 #ifdef _MSC_VER # ifdef NDEBUG # pragma comment(linker, "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup") # else # pragma comment(linker, "/SUBSYSTEM:CONSOLE") # endif #endif 

Use the entry point parameter to avoid linker errors if you specify:

 int main(int argc, char* argv[]) { ... } 
+4
source

Source: https://habr.com/ru/post/1380246/


All Articles