I know you can use CMake's configure_file
to make CMake variables available to your program. For example, I can use
#define ${CMAKE_BUILD_TYPE}
resulting in
#define Release
However, to keep my code more readible, I would prefer to define
#define BUILD_TYPE_RELEASE
Is there a simple way to achieve this?
Here is a fairly simple way to solve it:
In
CMakesLists.txt
:And in
config.h.in
:I am, however, still curious if there is a more elegant solution.
This is more a question of your preferred programming style (configuration files vs. compiler definitions).
In your case I would use
add_definitions()
or directly modify/appendCOMPILE_DEFINITIONS
directory property (using generator expressions also supports multi-configuration environments):Most simplified Debug/Release Check
You can also check what compiler definitions CMake does pre-define. Without having to modify/add something to your
CMakeLists.txt
files you could simply check forNDEBUG
definition (set for Release builds across platforms) in you C/C++ code:References