How to cmake install

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: To 'cmake install', you typically first configure your project using CMake, then build it using a build tool like Make or Ninja, and finally execute the install command. This process copies the compiled binaries, libraries, and other necessary files to their designated locations on your system, usually defined in the project's CMakeLists.txt file.

Key Facts

What is CMake and Why Use It?

CMake is a popular open-source, cross-platform build system generator. It doesn't build your software directly; instead, it generates native build files (like Makefiles for Make or Ninja build files for Ninja) that are then used by the actual build tools to compile your code. This abstraction allows developers to write build instructions once and have them work across various operating systems and compilers.

The 'cmake install' Process Explained

The 'install' step in the CMake workflow is crucial for making your compiled software usable on your system. After configuring and building your project, the installation process copies the built artifacts (executables, libraries, header files, documentation, etc.) to specific directories. This ensures that the software can be found and executed by the operating system or other programs.

Step 1: Configuration with CMake

Before you can install, you need to configure your project. This is done by running the cmake command, usually followed by the path to your project's source directory (or the build directory if you're doing an out-of-source build). A common command looks like this:

mkdir buildcd buildcmake ..

During this phase, CMake reads your project's CMakeLists.txt file, checks for dependencies, determines your system's capabilities, and generates the appropriate build files for your chosen generator (e.g., Makefiles). You can also specify configuration options here, such as the installation prefix:

cmake .. -DCMAKE_INSTALL_PREFIX=/path/to/install

The CMAKE_INSTALL_PREFIX variable is a fundamental CMake variable that dictates the base directory where files will be installed. If not specified, it often defaults to /usr/local on Unix-like systems, which is a standard location for user-compiled software to avoid conflicts with system packages.

Step 2: Building the Project

Once configured, you use your system's build tool to compile the project. If CMake generated Makefiles, you'd typically run:

make

If you used a different generator, like Ninja, you would run:

ninja

This step compiles all the source code and links it into executables and libraries.

Step 3: Installation

The final step is installation. This is where the install targets defined in the CMakeLists.txt file are invoked. You execute this using your build tool:

make install# orninja install

This command reads the installation rules specified in the CMakeLists.txt file. These rules tell CMake where to copy each file (e.g., executables go in bin, libraries in lib, headers in include, documentation in share/doc) relative to the CMAKE_INSTALL_PREFIX.

Defining Install Rules in CMakeLists.txt

The power of cmake install lies in the flexibility of defining installation rules within the CMakeLists.txt file. Developers use commands like install() to specify which files or directories should be installed, their destination, and other properties.

Here's a simplified example:

# Install executableinstall(TARGETS my_executable DESTINATION bin)# Install libraryinstall(TARGETS my_library DESTINATION lib)# Install header fileinstall(FILES include/my_header.h DESTINATION include)# Install data filesinstall(DIRECTORY data/ DESTINATION share/my_project/data)

The DESTINATION keyword specifies the subdirectory relative to CMAKE_INSTALL_PREFIX.

Common Issues and Best Practices

In summary, cmake install is the final step in the CMake build process that makes your compiled project available on your system by copying its components to designated locations, controlled by rules defined in the project's CMake configuration.

Sources

  1. cmake(1) Manual - CMakeBSD-3-Clause
  2. install — CMake 3.29.1 DocumentationBSD-3-Clause
  3. Install Documentation - CMake Community WikiCC-BY-SA-4.0

Missing an answer?

Suggest a question and we'll generate an answer for it.