介绍
这个示例展示了怎样去生成一个make install目标用于安装文件和二进制在你的系统上。例子基于上一个共享库示例。
本教程中的文件包括:
$ tree
.
├── cmake-examples.conf
├── CMakeLists.txt
├── include
│ └── installing
│ └── Hello.h
├── README.adoc
└── src
├── Hello.cpp
└── main.cpp
- CMakeLists.txt - 包含要运行的CMake命令
- cmake-examples.conf - 配置文件
- include/installing/Hello.h - 头文件
- src/Hello.cpp - 资源文件
- src/main.cpp - main文件
概念
安装
Cmake提供可以添加一个make install目标,允许用户去安装二进制,库或者其他文件。安装位置可以用变量CMAKE_INSTALL_PREFIX控制,调用方式cmake .. DCMAKE_INSTALL_PREFIX=/install/location
文件被安装和控制通过install( )函数。
install (TARGETS cmake_examples_inst_bin
DESTINATION bin)
将从cmake_examples_inst_bin生成的二进制文件并安装到${cmake_Install_PREFIX}/bin
install (TARGETS cmake_examples_inst
LIBRARY DESTINATION lib)
从cmake_examples_inst生成并安装共享库到${CMAKE_INSTALL_PREFIX}/lib
上面这些可能在windows上不工作,DLL目标你可能需要添加如下:
install (TARGETS cmake_examples_inst
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/
DESTINATION include)
将针对cmake_examples_inst库进行开发的头文件安装到${cmake_Install_PREFIX}/include目录中。
install (FILES cmake-examples.conf
DESTINATION etc)
安装配置文件到${CMAKE_INSTALL_PREFIX}/etc
在执行make install后,Cmake生成一个install_manifest.txt文件包含所有安装文件的细节。
你过你在root权限运行make install,install_manifest.txt文件将拥有root权限。
构建例子
$ mkdir build
$ cd build/
$ cmake ..
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/matrim/workspace/cmake-examples/01-basic/E-installing/build
$ make
Scanning dependencies of target cmake_examples_inst
[ 50%] Building CXX object CMakeFiles/cmake_examples_inst.dir/src/Hello.cpp.o
Linking CXX shared library libcmake_examples_inst.so
[ 50%] Built target cmake_examples_inst
Scanning dependencies of target cmake_examples_inst_bin
[100%] Building CXX object CMakeFiles/cmake_examples_inst_bin.dir/src/main.cpp.o
Linking CXX executable cmake_examples_inst_bin
[100%] Built target cmake_examples_inst_bin
$ sudo make install
[sudo] password for matrim:
[ 50%] Built target cmake_examples_inst
[100%] Built target cmake_examples_inst_bin
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/bin/cmake_examples_inst_bin
-- Removed runtime path from "/usr/local/bin/cmake_examples_inst_bin"
-- Installing: /usr/local/lib/libcmake_examples_inst.so
-- Installing: /usr/local/etc/cmake-examples.conf
$ cat install_manifest.txt
/usr/local/bin/cmake_examples_inst_bin
/usr/local/lib/libcmake_examples_inst.so
/usr/local/etc/cmake-examples.conf
$ ls /usr/local/bin/
cmake_examples_inst_bin
$ ls /usr/local/lib
libcmake_examples_inst.so
$ ls /usr/local/etc/
cmake-examples.conf
$ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib cmake_examples_inst_bin
Hello Install!
如果/usr/local/lib不在库路径中,则可能需要在运行二进制文件之前将其添加到该路径中。
附加说明
修改默认安装位置
如前所述,默认安装位置是用CMAKE_INSTALL_PERFIX设置的,默认为/usr/local/
如果要更改所有用户的默认位置,可以将以下代码添加到CMakeLists.txt文件顶层在添加任何二进制文件或库之前。
if( CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT )
message(STATUS "Setting default CMAKE_INSTALL_PREFIX path to ${CMAKE_BINARY_DIR}/install")
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE STRING "The path to use for make install" FORCE)
endif()
此示例将默认安装位置设置为在生成目录下。
目录
如果您希望在DESTDIR下安装所有文件,请指定此参数。
make install DESTDIR=/tmp/stage
这将为所有文件创建安装目录${DESTDIR}/${CMAKE_INSTALL_PREFIX},在这个例子中,将安装所有文件到/tmp/stage/usr/local路径下
$ tree /tmp/stage
/tmp/stage
└── usr
└── local
├── bin
│ └── cmake_examples_inst_bin
├── etc
│ └── cmake-examples.conf
└── lib
└── libcmake_examples_inst.so
Uninstall
默认CMake没有添加一个make uninstall目标,对于怎样生成卸载目标的细节请看FAQ
一个简单的方式如下:
sudo xargs rm < install_manifest.txt