Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use CMake to install

Tags:

cmake

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 .
like image 387
Aart Stuurman Avatar asked Jan 24 '18 17:01

Aart Stuurman


Video Answer


2 Answers

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.

like image 185
vre Avatar answered Oct 12 '22 04:10

vre


You can use the install command on your CMakeLists that will generate installation rules for your project. A basic example is shown bellow but check the cmake documentation if you need something more complex.

project (Test)

add_executable(test main.cpp)

install(TARGETS test DESTINATION bin)

Then after generate the makefiles you can ust type sudo make install and the test application will be installed on system bin folder.

like image 21
K. Stopa Avatar answered Oct 12 '22 04:10

K. Stopa