I would suggest you to set the definition using target_compile_definitions
:
target_compile_definitions(myexe PUBLIC foo="bar")
To set it to different things depending if it's release or debug, using generator expressions would make those options portable when using a multi config generator:
target_compile_definitions(myexe PUBLIC
$<$<CONFIG:Debug>:foo="bar">
$<$<CONFIG:Release>:baz="bat">
)
If you really don't want the bar
option to be present in your repository including the CMake files, there are ways to define that too.
You can send it as part of a command line option. For thst you can define that command line option using the option
command, then send the command line argument using a preset:
option(MYPROJECT_MY_OPTION "Activate the definitions" OFF)
if(MYPROJECT_MY_OPTION)
target_compile_definitions(myexe PUBLIC
$<$<CONFIG:Debug>:foo=bar">
$<$<CONFIG:Release>:baz="bat">
)
endif()
Then create some profiles:
{
"version": 2,
"cmakeMinimumRequired": {
"major": 3,
"minor": 22,
"patch": 0
},
"configurePresets": [
{
"name": "cmake-pedantic",
"hidden": true,
"warnings": {
"dev": false,
"deprecated": true,
"uninitialized": true,
"unusedCli": true,
"systemVars": false
},
"errors": {
"dev": false,
"deprecated": true
}
},
{
"name": "generator-ninja",
"hidden": true,
"generator": "Ninja Multi-Config",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"CMAKE_DEBUG_POSTFIX": "d",
"CMAKE_MAKE_PROGRAM": "ninja"
}
},
{
"name": "dev",
"displayName": "Development",
"description": "Development preset",
"inherits": ["generator-ninja"]
},
{
"name": "dev-with-option",
"displayName": "Development",
"description": "Development preset",
"binaryDir": "${sourceDir}/build-with-option",
"inherits": ["generator-ninja"],
"cacheVariables": {
"MYPROJECT_MY_OPTION": true
}
}
],
"buildPresets": [
{
"name": "dev-debug",
"displayName": "Debug",
"description": "Build with debug informations",
"configuration": "Debug",
"configurePreset": "dev"
},
{
"name": "dev-relwithdebinfo",
"displayName": "RelWithDebInfo",
"description": "Build with debug informations and optimizations enabled",
"configuration": "RelWithDebInfo",
"configurePreset": "dev"
},
{
"name": "dev-release",
"displayName": "Release",
"description": "Build with optimizations enabled",
"configuration": "Release",
"configurePreset": "dev"
},
{
"name": "dev-with-option-debug",
"displayName": "Debug",
"description": "Build with debug informations",
"configuration": "Debug",
"configurePreset": "dev-with-option"
},
{
"name": "dev-with-option-relwithdebinfo",
"displayName": "RelWithDebInfo",
"description": "Build with debug informations and optimizations enabled",
"configuration": "RelWithDebInfo",
"configurePreset": "dev-with-option"
},
{
"name": "dev-with-option-release",
"displayName": "Release",
"description": "Build with optimizations enabled",
"configuration": "Release",
"configurePreset": "dev-with-option"
}
],
"testPresets": [
{
"name": "base-test",
"hidden": true,
"output": {
"outputOnFailure": true
},
"execution": {
"noTestsAction": "error",
"stopOnFailure": true
}
},
{
"name": "dev-debug",
"displayName": "Debug",
"configuration": "Debug",
"configurePreset": "dev",
"inherits": "base-test"
},
{
"name": "dev-relwithdebinfo",
"displayName": "RelWithDebInfo",
"configuration": "RelWithDebInfo",
"configurePreset": "dev",
"inherits": "base-test"
},
{
"name": "dev-release",
"displayName": "Release",
"configuration": "Release",
"configurePreset": "dev",
"inherits": "base-test"
},
{
"name": "dev-with-option-debug",
"displayName": "Debug",
"configuration": "Debug",
"configurePreset": "dev",
"inherits": "base-test"
},
{
"name": "dev-with-option-relwithdebinfo",
"displayName": "RelWithDebInfo",
"configuration": "RelWithDebInfo",
"configurePreset": "dev",
"inherits": "base-test"
},
{
"name": "dev-with-option-release",
"displayName": "Release",
"configuration": "Release",
"configurePreset": "dev",
"inherits": "base-test"
}
]
}
This will allow IDEs to select between dev
and dev-with-option
, and also allow the IDE to select debug or release configs.