I'm trying to get CMake to build into a directory 'build', as in project/build
, where the CMakeLists.txt is in project/
.
I know I can do:
mkdir build cd build cmake ../
but that is cumbersome. I could put it in a script and call it, but then it's unpleasant to provide different arguments to CMake (like -G "MSYS Makefiles"), or I would need to edit this file on each platform.
Preferably I would do something like SET(CMAKE_OUTPUT_DIR build)
in the main CMakeLists.txt. Please tell me that this is possible, and if so, how? Or some other out of source build method that makes it easy to specify different arguments?
In order to build CMake from a source tree on Windows, you must first install the latest binary version of CMake because it is used for building the source tree. Once the binary is installed, run it on CMake as you would any other project.
To use out of source builds, create a build directory in your top-level folder (technically, this can be anywhere, but the top-level project folder seems to be a logical choice). Next, change into your build directory and run cmake pointing it to the directory of the top-level CMakeLists. txt.
Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.
If you create the cache variable in the CMakeLists. txt file and then pass the argument via calling cmake, won't the CMakeList. txt file keep overwriting the argument value? No, the cache is populated on the first run with either the default value, or the value supplied on the command line if it is provided.
CMake 3.13 or newer supports the command line options -S
and -B
to specify source and binary directory, respectively.
cmake -S . -B build -G "MSYS Makefiles"
This will look for the CMakeLists.txt
in the current folder and create a build
folder (if it does not yet exist) in it.
For older versions of CMake, you can use the undocumented CMake options -H
and -B
to specify the source and binary directory upon invoking cmake
:
cmake -H. -Bbuild -G "MSYS Makefiles"
Note that there must not be a space character between the option and the directory path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With