44

I want to tell CMake to output files and folders to a different folder instead of the current folder. I'm talking about the generated files by CMake below:

  • file: CMakeCache.txt
  • dir: CMakeFiles/
  • file: Makefile
  • dir: bin/
  • file: cmake_install.cmake

Is there a way to let CMake output these files and folders in another folder? I wrote a tool that executes CMake from the root of the project-directory, as a result my project-directory gets messed up with the generated files and folders listed above.

Here a link what I want: http://pastebin.com/cxykCi5M

Hope this will clarify more what I want.

AndaluZ
  • 1,410
  • 3
  • 15
  • 33
  • [This](http://stackoverflow.com/questions/18826789/cmake-output-build-directory) post may be of some assistance. – Rubens Dec 16 '13 at 12:57

1 Answers1

89

You can use the undocumented command line options -B and -H to specify your build directory and source directory respectively. So, from your project's root, you can do:

cmake -Bbuild -H.

(Where build is your build directory path.)

randomusername
  • 7,927
  • 23
  • 50
Fraser
  • 74,704
  • 20
  • 238
  • 215
  • 1
    @Fraser is it possible to specify the build folder as an option in `CMakeLists.txt`? – Steve Lorimer Jan 26 '16 at 20:25
  • @SteveLorimer - No, I'm afraid not. It's pretty much up to the user, although there are some tricks to mitigate against users trying to do an in-source build for example if that's what you're really after. – Fraser Jan 27 '16 at 02:09
  • 6
    Thank you for this hint... this helps me ... no "md XX;cd XX;cmake .." anymore ;) thank you ;) – Gabor Jun 05 '16 at 09:05
  • if they are undocumented, are they safe to use? – jwm Dec 19 '18 at 02:01
  • 1
    `-B` is documented, at least in CMake 3.14: `-B = Explicitly specify a build directory.` Of course you can display it with `cmake --help` – air-dex Apr 19 '19 at 02:05
  • As of CMake 3.13, the `-B` is now documented. They also claim to use `-S` when providing the source directory, although the still undocumented `-H` option works also. – Kevin Oct 04 '19 at 12:15