CMake visual studio debug / release configuration

I am creating a project for my visual studio to use CMake, but I have two problems that I have not yet been able to solve.

1 How to set the preprocessor option for Release and another for Debug?

2 I have a project with opengl and directx, so for DebugOpenGL and ReleaseOpenGL I want to exclude all directx cpp / h files from buld. With DebugDirectX and ReleaseDirectx, opengl files are excluded. How to set it up?

EDIT:

Here is what I got for 1. so far:

cmake_minimum_required(VERSION 2.8) project(TEngine) if(CMAKE_CONFIGURATION_TYPES AND MSVC) #DebugOpenGL flags set(CMAKE_CXX_FLAGS_DEBUGOPENGL "/D_DEBUG /MDd /Zi /Ob0 /Od /RTC1" CACHE STRING "Flags used by the C++ compiler during maintainer builds." FORCE) set(CMAKE_C_FLAGS_DEBUGOPENGL "/D_DEBUG /MDd /Zi /Ob0 /Od /RTC1" CACHE STRING "Flags used by the C compiler during maintainer builds." FORCE) set(CMAKE_EXE_LINKER_FLAGS_DEBUGOPENGL "/debug /INCREMENTAL" CACHE STRING "Flags used for linking binaries during maintainer builds." FORCE ) set(CMAKE_SHARED_LINKER_FLAGS_DEBUGOPENGL "/debug /INCREMENTAL" CACHE STRING "Flags used by the shared libraries linker during maintainer builds." FORCE ) #ReleaseOpenGL flags set(CMAKE_CXX_FLAGS_RELEASEOPENGL "/MD /O2 /Ob2 /D NDEBUG" CACHE STRING "Flags used by the C++ compiler during maintainer builds." FORCE) set(CMAKE_C_FLAGS_RELEASEOPENGL "/MD /O2 /Ob2 /D NDEBUG" CACHE STRING "Flags used by the C compiler during maintainer builds." FORCE) set(CMAKE_EXE_LINKER_FLAGS_RELEASEOPENGL "/INCREMENTAL:NO" CACHE STRING "Flags used for linking binaries during maintainer builds." FORCE ) set(CMAKE_SHARED_LINKER_FLAGS_RELEASEOPENGL "/INCREMENTAL:NO" CACHE STRING "Flags used by the shared libraries linker during maintainer builds." FORCE ) #DebugDirectX flags set(CMAKE_CXX_FLAGS_DEBUGDIRECTX "/D_DEBUG /MDd /Zi /Ob0 /Od /RTC1" CACHE STRING "Flags used by the C++ compiler during maintainer builds." FORCE) set(CMAKE_C_FLAGS_DEBUGDIRECTX "/D_DEBUG /MDd /Zi /Ob0 /Od /RTC1" CACHE STRING "Flags used by the C compiler during maintainer builds." FORCE) set(CMAKE_EXE_LINKER_FLAGS_DEBUGDIRECTX "/debug /INCREMENTAL" CACHE STRING "Flags used for linking binaries during maintainer builds." FORCE ) set(CMAKE_SHARED_LINKER_FLAGS_DEBUGDIRECTX "/debug /INCREMENTAL" CACHE STRING "Flags used by the shared libraries linker during maintainer builds." FORCE ) #ReleaseDirectx flags set(CMAKE_CXX_FLAGS_RELEASEDIRECTX "/MD /O2 /Ob2 /D NDEBUG" CACHE STRING "Flags used by the C++ compiler during maintainer builds." FORCE) set(CMAKE_C_FLAGS_RELEASEDIRECTX "/MD /O2 /Ob2 /D NDEBUG" CACHE STRING "Flags used by the C compiler during maintainer builds." FORCE) set(CMAKE_EXE_LINKER_FLAGS_RELEASEDIRECTX "/INCREMENTAL:NO" CACHE STRING "Flags used for linking binaries during maintainer builds." FORCE ) set(CMAKE_SHARED_LINKER_FLAGS_RELEASEDIRECTX "/INCREMENTAL:NO" CACHE STRING "Flags used by the shared libraries linker during maintainer builds." FORCE ) set(CMAKE_CONFIGURATION_TYPES "DebugOpenGL;ReleaseOpenGL;DebugDirectX;ReleaseDirectX") set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}" CACHE STRING "Reset the configurations to what we need" FORCE) if(CMAKE_BUILD_TYPE STREQUAL "DebugOpenGL") add_definitions(/DTE_USE_OPENGL) endif() if(CMAKE_BUILD_TYPE STREQUAL "ReleaseOpenGL") add_definitions(/DTE_USE_OPENGL) endif() if(CMAKE_BUILD_TYPE STREQUAL "DebugDirectX") add_definitions(/DTE_USE_OPENGL) endif() if(CMAKE_BUILD_TYPE STREQUAL "ReleaseDirectX") add_definitions(/DTE_USE_OPENGL) endif() endif() set(Boost_ADDITIONAL_VERSIONS "1.47" "1.47.0") set(BOOST_ROOT "${TEngine_SOURCE_DIR}/Externals/boost_1_47_0") if(WIN32) add_definitions(-DTEWINDOWS) elseif(LINUX) add_definitions(-DTELINUX -DTE_USE_OPENGL) endif() subdirs(TEEngineTest TECore TEGraphics TEPhysics TEngine) 
+4
source share
3 answers

1

 if(CMAKE_BUILD_TYPE STREQUAL "Debug") add_definitions(/DYOURDEFINITION) endif() 

2: If I understood you correctly, you need to create an optional variable

 option(USE_OPENGL "Use OpenGL or DirectX") 

And then check it out when you add a new target:

 if(USE_OPENGL) add_subdirectory(OpenGL) else() add_subdirectory(DirectX) endif() 

Of course, you should create CMakeLists.txt in OpenGL / and DirectX / dirs.

+1
source
  • How to set preprocessor option for Release and another for Debug?

The right way to do this is to set the COMPILE_DEFINITIONS property in the correct area using generator expressions.

If you want to add definitions to all goals, for example add_definitions() , then use:

  set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:DebugOpenGL>:_DEBUG> $<$<CONFIG:ReleaseOpenGL>:NDEBUG> $<$<CONFIG:DebugDirectX>:_DEBUG> $<$<CONFIG:ReleaseDirectX>:NDEBUG> ) set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:DebugOpenGL>:TE_USE_OPENGL> $<$<CONFIG:ReleaseOpenGL>:TE_USE_OPENGL> $<$<CONFIG:DebugDirectX>:TE_USE_DIRECTX> $<$<CONFIG:ReleaseDirectX>:TE_USE_DIRECTX> ) 

You can also set COMPILE_DEFINITIONS only for target or a specific source file . See set_property .

To set other compilation flags, you can set the COMPILE_OPTIONS property in a directory, target, or source file, as described above. However, you can also use the add_compile_options() command, because unlike add_definitions() it supports generator expressions. You can set COMPILE_OPTIONS for goals, and also use target_compile_options() . For source files with CMake 3.3, you still need to set the COMPILE_FLAGS property.

  set(MY_DEBUG_OPTIONS /MDd /Zi /Ob0 /Od /RTC1) set(MY_RELEASE_OPTIONS /MD /Ob2 /O2) add_compile_options( "$<$<CONFIG:DebugOpenGL>:${MY_DEBUG_OPTIONS}>" "$<$<CONFIG:ReleaseOpenGL>:${MY_RELEASE_OPTIONS}>" "$<$<CONFIG:DebugDirectX>:${MY_DEBUG_OPTIONS}>" "$<$<CONFIG:ReleaseDirectX>:${MY_RELEASE_OPTIONS}>" ) 

Note that quotation marks in add_compile_options() are required to place lists of options inside these generator expressions.

  1. I have a project with opengl and directx, so for DebugOpenGL and ReleaseOpenGL I want to exclude all directx cpp / h files from buld. With DebugDirectX and ReleaseDirectx, opengl files are excluded. How to set it up?

This can be a problem. Although you can use generator expressions to add source files to the target system, I do not know any IDEs that support source files for each configuration. Visual Studio and the Visual Studio CMake generator do not support it. Xcode and the Xcode generator also do not support this. Therefore, if you want to create using Visual Studio or another IDE, you cannot do this.

Generators that use one configuration at a time, such as makefile or ninja generators, should support this simply.

Instead of making the source files different from each other, there are several other options. Here is a couple.

You can have several goals and the source is different from them:

 set(OPENGL_SOURCES src/opengl/func1.cpp src/opengl/func2.cpp) set(DIRECTX_SOURCES src/opengl/func1.cpp src/opengl/func2.cpp) add_executable(opengl_foo ${COMMON_SOURCES} ${OPENGL_SOURCES}) add_executable(directx_foo ${COMMON_SOURCES} ${DIRECTX_SOURCES}) 

Note that this means that you can return to the Debug and Release settings, which will simplify the rest of your configuration.

Alternatively, you can save separate OpenGL and DirectX configurations using some indirectness in the source code. Have several 'dummy' cpp files and header files that switch between #include with your preprocessor opengl or directx files:

 // src/func1.h #ifdef TE_USE_OPENGL # include "opengl/func1.h.inc" #elif defined(TC_USE_DIRECTX) # include "directx/func1.h.inc" #endif // src/func1.cpp #ifdef TE_USE_OPENGL # include "opengl/func1.cpp.inc" #elif defined(TC_USE_DIRECTX) # include "directx/func1.cpp.inc" #endif 

And then just link to these dummy files in CMake:

 add_executable(foo src/func1.h src/func1.cpp) 

Now creating different configurations will lead to code loss. Also, if you go and rename files to the end in .inc , you can even reference them in add_executable() so that they appear in VS, but VS will not try to create them directly.

+9
source
 if(CMAKE_BUILD_TYPE STREQUAL "Debug") add_definitions(/DYOURDEFINITION) endif() 

will not work in the context of MSVS multi-config. For this you need

 set(MY_DEFINITION $<$<CONFIG:Debug>:definition_for_debug> $<$<CONFIG:RelWithDebInfo>:definition_for_rel_with_debug> $<$<CONFIG:Release>:definition_for_release> $<$<CONFIG:MinSizeRel>:definition_for_tight_release> ) 
+7
source

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


All Articles