I have several build profiles in CLion -> Settings -> Build, Execution, Deployment -> CMake
. How can I set the preprocessor definitions for each profile, regardless of which compiler I use?
Asked
Active
Viewed 5,287 times
5

volodya7292
- 454
- 8
- 20
2 Answers
11
- Go to
Settings -> Build, Execution, Deployment -> CMake
. - Select the profile for which you want to set preprocessor definition.
- Write in the
CMake options
field:-DYOUR_DEFINE_VARIABLE=1
- In your CMakeLists.txt write the following:
if (YOUR_DEFINE_VARIABLE)
add_definitions(-DYOUR_DEFINE_VARIABLE=1)
endif (YOUR_DEFINE_VARIABLE)

volodya7292
- 454
- 8
- 20
-
Newbie here, why does it neet to be set both in clion buid config and cmakelists ? – Xmanoux Dec 24 '22 at 09:55
-
1@Xmanoux As I understand, `-D` in CMake options just adds a local variable to cmake script, whereas `add_definitions` adds definitions (`-D`) to the compiler command line. More information: [add_definitions](https://cmake.org/cmake/help/latest/command/add_definitions.html), [cmake options](https://cmake.org/cmake/help/v3.24/manual/cmake.1.html#options) – volodya7292 Dec 25 '22 at 13:13
0
I didn't try this but this should work
if (CMAKE_CONFIGURATION_TYPES)
string(TOLOWER "${CMAKE_CONFIGURATION_TYPES}" CMAKE_CONFIGURATION_TYPES_LOWER)
else()
string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_CONFIGURATION_TYPES_LOWER)
endif()
foreach(config ${CMAKE_CONFIGURATION_TYPES_LOWER})
if (${config} MATCHES "debug")
#add preprocessor definition something like this bellow
add_definitions(-DFOO -DBAR ...)
elseif(${config} MATCHES "release")
#and so on...
endif()
endforeach()
So, to correctly check strings, we convert build configuration types to lower case, and then we check if config from the type matches this custom configuration types. if it does, then we can add some preprocessor definitions, etc (

golobitch
- 1,466
- 3
- 19
- 38
-
-
${CMAKE_CONFIGURATION_TYPES} outputs nothing. I just realized that the `CMake` tab contains not cmake configurations, but build profiles. – volodya7292 Jul 14 '19 at 13:50
-
1Global `add_definitions` is a bad style in modern CMake. It's adviseable to use `target_compile_definitions` etc instead. – Victor Sergienko Jul 16 '19 at 17:34
-