- 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.
- 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
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.