1

The question Debug vs Release in CMake indicates that

cd ~/codebase
mkdir Release
cd Release
cmake -DCMAKE_BUILD_TYPE=Release ..
make

Will create the Makefile in release, and build the binary there. The intermediate .o files will be in a subdirectory of this.

However, when I do this with my project, CMake ignores the PWD that it is started from. The final target is always the directory ~/codebase/ which contains CMakeList.txt. In the cmake-gui tool, I specified the source and build directories to be the same directory, the FQN to codebase

I'm new to CMake, and don't know how to get this to work as I expect. What should I modify to get this work as expected?

CSM
  • 1,232
  • 1
  • 8
  • 12
  • If you want your build to be in a subfolder, why do you specify the source and build to the same directory? " In the cmake-gui tool, I specified the source and build directories to be the same directory" In the gui, you should point the source to the folder with CMakeLists.txt, and point build to a different folder (doesn't matter if it is a subfolder or it is in separate folder elsewhere). – fern17 Nov 06 '20 at 14:29
  • I want to also do a debug build, by replacing `Release` with `Debug` in my commands. I don't want to hardwire the Release subdirectory into the cmakecache.txt file – CSM Nov 06 '20 at 14:33
  • you need to have a different folder for each debug and release builds... There will be a CMakeCache.txt file inside of both debug and release folders. – fern17 Nov 06 '20 at 14:42

1 Answers1

3

If you are using a single configuration generator (Ninja/Unix-Makefiles)

Then you need a build folder for each configuration.

Like this:

# Configure the build
cmake -S . -B build/Debug -D CMAKE_BUILD_TYPE=Release

# Actually build the binaries
cmake --build build/Debug

For multi-configuration generators it's slightly different (Ninja Multi-Config, Visual Studio)

# Configure the build
cmake -S . -B build

# Actually build the binaries
cmake --build build --config Debug

If you are wondering why this is necessary it's because cmake isn't a build system. It's a meta-build system (IE a build system that creates build systems). This is basically the result of handling build systems that support multiple-configurations in 1 build. If you'd like a deeper understanding I'd suggest reading a bit about cmake in Craig Scott's book "Professional CMake: A Practical Guide

Note: My examples use newer cmake cli practices.

EDIT: That question you linked to has dangerously out of date answers...

  • `s/a build system that build's build systems/a build system generator/` – Mizux Nov 07 '20 at 09:21
  • 1
    Thanks, I've got it working. In the end I did `set -e && mkdir -p build/release && cd build/release && cmake -G "Unix Makefiles" -D CMAKE_C_COMPILER=clang -D CMAKE_CXX_COMPILER=clang++ -D CMAKE_BUILD_TYPE=RelWithDebInfo ../.. && make VERBOSE=1 ` – CSM Nov 16 '20 at 15:30