6

Is it possible to specify build directory within CMakeLists file? If yes, how.

My aim is to be able to call "cmake" within top level source directory and have cmake figure out the build directory.

Amani
  • 16,245
  • 29
  • 103
  • 153

2 Answers2

6

Afaik, with CMake the build directory is always the directory from where you invoke the cmake or ccmake command. So if you want to change the build directory, you have to change directories before running CMake.

To control the location where executables, static and shared libraries are placed once finished, you can modifiy CMAKE_RUNTIME_OUTPUT_DIRECTORY, CMAKE_ARCHIVE_OUTPUT_DIRECTORY, and CMAKE_LIBRARY_OUTPUT_DIRECTORY respectively.

  • Yes, I agree with you about the first point, but my purpose is to be able to avoid to "cd" to build directory every time I make changes to CMakeLists. – Amani Dec 30 '13 at 08:07
  • Can't you try an in-source build (although it's generally frowned upon, it might be what you need in this case)? Other than that, it is not actually a matter of cmake but of `make` itself - from wherever you invoke `make`, that's the build directory. – Michael Schlottke-Lakemper Dec 30 '13 at 08:22
3

By design, there is not a way to specify that in CMakeLists.txt. It is designed for the user to be able to build the project in whatever directory they want. The typical workflow is:

  1. Check out the project source code.
  2. Go to desired build directory, or the source dir if you plan to do an in-source build.
  3. Run cmake or ccmake to configure the project in that build directory.
  4. Build your project.

All of the directories specified within your CMakeLists.txt should be relative to the ${PROJECT_BINARY_DIR} and ${PROJECT_SOURCE_DIR} variables. In this way, your code becomes buildable across different platforms, which is the goal of CMake.

zjm555
  • 781
  • 1
  • 5
  • 14
  • Agree, but then in my opinion being able to specify build directory within CMakeLists.txt could be made as an option, which will still maintain building across different platforms. e.g. instead of doing "cmake ../build", one could have an option within CMakeLists.txt which goes like "set(CMAKE_BUILD_DIRECTORY ../build)", and since this goes into the project's top CMakeLists.txt it should work. – Amani Dec 31 '13 at 06:07
  • Feel free to request this feature on our [public issue tracker](http://www.cmake.org/Bug). I'm not sure it will be well-received since it sort of goes against the usual CMake model of configuration, but you can get more community discussion there. – zjm555 Dec 31 '13 at 13:29