I have just acquired an arbitrary CMake project from the internet and I am not sure how to compile it. What commands do I need to run to build it from the command line?
-
1There are roughly a million variants of this question here on SO and I could not find a canonical duplicate. This is my attempt at contributing such a canonical question/answer. – Alex Reinking May 06 '21 at 20:31
-
1Agreed, I really like this question because although there are many questions about how to start using CMake many of these questions are not a good example to use as a duplicate for the more general question of how to get started. – drescherjm Jan 13 '23 at 17:02
1 Answers
Basic steps
If you're on a Unix-y operating system, like Linux or macOS, then you would run:
$ cmake -DCMAKE_BUILD_TYPE=Release -S /path/to/source-dir -B /path/to/build-dir
$ cmake --build /path/to/build-dir
Here, /path/to/source-dir
is the directory containing the root-level CMakeLists.txt
, this is most commonly the root of a source control repository. Meanwhile, /path/to/build-dir
is a distinct directory (that does not need to exist yet) that CMake will use to store the generated build system and its outputs. This is called an out-of-tree build. You should never attempt an in-tree build with CMake because of the possibility of name clashes and difficulty involved with cleaning up the generated files.
When building with a single-config generator (like Make, which is the default on Unix), you specify the build type by setting the CMAKE_BUILD_TYPE
variable in the first command, known as the configure step. You must always set this variable when working with a single-config generator. The built-in configs are Debug
, Release
, RelWithDebInfo
, and MinSizeRel
. See this answer for more detail on this.
After the configure step, you may build the project by either calling the underlying build tool (in this case, make
) or by calling CMake's generic build launcher command (cmake --build
), as I do here.
If you're on Windows, then the default generator is Visual Studio, which is a multi-config generator. This means the build type is chosen during the build step rather than the configure step, and the commands must be adjusted accordingly:
$ cmake -S /path/to/source-dir -B /path/to/build-dir
$ cmake --build /path/to/build-dir --config Release
These steps assume that the CMake build you are looking at is well behaved. If a project fails to build with the above steps and you have all of its dependencies installed to system locations (and they are well behaved), then you should open an issue with the upstream project. The most common source of bad behavior in mature CMake builds is dependency handling. Too often you will have to read the build or its documentation to determine which variables need to be set (via -D
, like we did with CMAKE_BUILD_TYPE
above) for the project to find its dependencies.
Advanced topics
Setting options and cache variables
Some projects offer options to enable/disable tests, components, features, etc. These are typically done by writing entries to the CMake cache during the configure step. For example, a common way to disable building tests is to set BUILD_TESTING
to NO
at the command line:
$ cmake -S /path/to/source-dir -B /path/to/binary-dir [...] -DBUILD_TESTING=NO
This particular variable is a convention, but is not guaranteed to be honored. Check the project's documentation to see which options are available.
Selecting a generator and toolchain
When using the Visual Studio generators specifically, you can tell CMake which platform you wish to target and which version of the compiler you would like to use. The full form of the CMake configure command for this is:
$ cmake -G "Visual Studio 16 2019" -A <ARCH> -T<TOOLSET> [...]
Valid values of <ARCH>
include Win32
, x64
, ARM
, and ARM64
. If <TOOLSET>
is not specified, then the 32-bit MSVC compiler will be used. Typically, you will want this to be host=x64
to ensure that 64-bit MSVC is used, which can allocate more memory for large linking steps. You can also set <TOOLSET>
to ClangCL
to use the Visual Studio provided ClangCL tools.
On all generators, CMake sniffs the environment for which compiler to use. It checks the CC
and CXX
environment variables for the C and C++ compilers, respectively. If those are empty, it will look for cc
and c++
executables in the PATH
. You can manually override the compilers by setting the CMAKE_C_COMPILER
and CMAKE_CXX_COMPILER
CMake cache (not environment) variables at the CMake command line (using -D
again).
Installing & using dependencies
Once a CMake project has been built, you may install it either systemwide or (preferably) to a local prefix by running:
$ cmake --install /path/to/build-dir --prefix /path/to/install-dir [--config Release]
Where --config
is only required if a multi-config generator was used. Once installed to a local prefix, a project that depends on it may be configured by setting CMAKE_PREFIX_PATH
to /path/to/install-dir
.

- 16,724
- 5
- 52
- 86