Search code examples
c++cbuild-processcmake

How do I tell cmake where to output its build data?


How do I tell cmake where it should output its build data?

Let's say I have a dir with the source code called src/, and then since cmake outputs a lot of files I would like him to put all of that in a dir called build/. BUT I would like him to put the generated Makefile in the project root, so I don't have to go into the build dir to build the application.

Is this possible with cmake?

I have managed to get the cmake out put if I fun cmake in the build dir like this:

cd build/
cmake ../src/
make
./hello 

But it would be nice to stay in the project root and type something like this

cmake
make 
./hello

I guess that I need to put a CMakeList.txt in the project root with some magic commands telling him where he could put the object files and where he can find the source code.

Thanks


Update: Since my question is a little bit vague.

After I have run the cmake commands this is how I would like my tree to look like:

src/CMakeLists.txt
src/hello.c 
src/hello.h

build/CMakeCache.txt  
build/CMakeFiles/
build/cmake_install.cmake  

CMakeLists.txt
Makefile

So the question is how should the CMakeLists.txt look like in this setup.

  • CMakeLists.txt
  • src/CMakeLists.txt

But maybe that is not possible?


Solution

  • BUT I would like him to put the generated Makefile in the project root, so I don't have to go into the build
    dir to build the application.

    cmake not designed for that, as I know, BUT you can stay in the project root and type:
    make -C build
    ./hello with custom build rules or set_target_properties, you can force cmake to put result executable to sources directory or you can use ./build/hello

    Type "cd build && cmake .." you need only once, after that make will automaticaly start cmake, if something changed.