Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting CMake to build out of source without wrapping scripts

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?

like image 414
Max Avatar asked Jun 21 '12 16:06

Max


People also ask

How do I build from source CMake?

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.

How do I get out of source build?

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.

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

How do you pass command line arguments in CMake?

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.


1 Answers

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.

like image 111
sakra Avatar answered Oct 22 '22 15:10

sakra