Thursday, July 28, 2022

[SOLVED] Is it possible to rename cmake's build directory?

Issue

cmake uses ./build as a standard place to generate build system and to place all intermediate files. Is this a hard requirement or is it possible to use a different name such as ./work? or maybe ./work/build?

To put in context this question. This is an initial idea so I can keep the same top level workflow for different project languages and toolchains.


Solution

Yes. The notion that CMake has a "standard" binary directory is incorrect. There is no default. Using build is common, but it is truly just a convention. In general, you can run:

$ cmake -G Ninja -S /path/to/source-dir -B /path/to/binary-dir -DCMAKE_BUILD_TYPE=RelWithDebInfo [...more options...]

Where /path/to/source-dir is the path to the folder containing the top-level CMakeLists.txt. People commonly run CMake from this folder, so this is often just -S .. Similarly, the binary directory, /path/to/binary-dir, is commonly stored beneath the source directory in a folder called build. So in these cases you will see -B ./build.

The values of both flags may be arbitrary, however.

For instance, when building LLVM, I often write:

$ cmake ... -S llvm -B build ... 

because LLVM's "top-level" CMakeLists.txt is actually in the llvm/ subfolder of their monorepo structure. But I still keep the build directory at the top level. I could just as easily have written:

$ cmake ... -S llvm -B $HOME/.binary-trees/llvm ...

Which is a little odd, but perfectly valid.



Answered By - Alex Reinking
Answer Checked By - David Goodson (WPSolving Volunteer)