cmake

How to use CMake to install


I can build my projects successfully with CMake, but can I use it to install the results?

With Make I add the target install and call that from the command line. I cannot figure out if this is possible with CMake.

The final goal is to install a static library, dynamic library and corresponding header files in a platform-portable way. How I imagine it would work: On Linux, copy to /usr/include and /usr/lib. On Windows it would probably be a user-provided folder with an include and lib folder.

The install rule suggests that something like this is possible. But how do I actually use it?

Currently I do the following:

  1. mkdir build
  2. cd build
  3. cmake ..
  4. cmake --build .

Here I would expect to do something like this:

  1. cmake --install .

Solution

  • You can use the command cmake --build . --target install --config Debug for installation.

    CMake's build tool mode supports further arguments that are of interest in this case. You can select the target to build by --target option, the configuration to build by --config option, and pass arguments to the underlying build tool by means of the -- option. See the documentation (Build Tool Mode) for the build-tool-mode.

    In CMake 3.15 and newer, you can use the simpler cmake --install command to Install a Project:

    cmake --install . --config Debug

    It additionally supports --prefix, --component and --strip.