cmakeout-of-source

Proper way to get an out-of source build in multi-part cmake project


I use cmake to build, test and install my project. My way is a little crude: I use a scripts like this to get an out-of-source build:

DIR=debug_build &&
rm -fr ./${DIR} &&
mkdir -p ${DIR} &&
cd ${DIR} &&
cmake -D CMAKE_BUILD_TYPE=Debug $@ .. &&
make 

I guess I could at least put it into makefile, but isnt it strange to use Makefile to run cmake to generate new Makefiles and run another make?

What is the proper way to do it? Does cmake have an option to avoid calling "cd"?

Recently it became even more complicated as I factoread out a library that I would like to compile, test and distribute separately. I want to avoid duplication of all the stuff I have in my top CMakeLists.txt.

How to do it properly with cmake?


Solution

  • No, CMake does not have any such option. I usually like to have a wrapper Makefile to control CMake-invocations.

    The main issue here is that the Makefile-generator doesn't support specifying the configuration when running make, as the Visual Studio generator does. I think there is a feature request filed for this, but it would be very invasive procedure to fix.