Is there some system variable in CMakeLists.txt to set all build temporary files to specified dir (CMakeCache, CMakeFiles, build artifacts, etc)? This can be done using command line parameter -B (path-to-build), but I need it inside CMakeLists.txt to set default dir for all files, which are not-tracked with git
2 Answers
If you specify a build directory via command line, it always overwrites any values from other sources.
You can achieve something similar to the effect you desire using cmake presets, but it's a relatively new addition (= version 3.19) to the cmake features and it doesn't specify the build directory inside the CMakeLists.txt
file, but inside a CMakePresets.json
file.
The presence of the file won't break the project for older cmake versions, but the initial configuration becomes less convenient.
CMakePresets.json (place next to the toplevel CMakeLists.txt
)
{
"version": 1,
"cmakeMinimumRequired": {
"major": 3,
"minor": 19,
"patch": 0
},
"configurePresets": [
{
"name": "makefiles",
"displayName": "Default configuration",
"description": "Default config",
"generator": "Unix Makefiles",
"binaryDir": "../build"
}
]
}
This allows you to choose the default build location as a sibling directory of the source dir using
cmake --preset makefiles .
This uses the sibling directory build
of the source directory as the binary directory.

- 80,457
- 12
- 86
- 114
You can use CMake presets for it. You use the binaryDir
property and never again you need to provide the -B
argument as long as you use presets to generate your project files and/or build your project.

- 13,100
- 5
- 45
- 79