CMake基础 第5节 安装项目
介紹#
此示例說明如何生成make install目標以在系統上安裝文件和二進制文件。這基于前面的共享庫示例。
本教程中的文件如下:
$ tree . ├── cmake-examples.conf ├── CMakeLists.txt ├── include │ └── installing │ └── Hello.h ├── README.adoc └── src├── Hello.cpp└── main.cpp-
[CMakeLists.txt] - 包含你希望運行的 CMake 命令
cmake_minimum_required(VERSION 3.5)project(cmake_examples_install)############################################################ # Create a library #############################################################Generate the shared library from the library sources add_library(cmake_examples_inst SHAREDsrc/Hello.cpp )target_include_directories(cmake_examples_instPUBLIC ${PROJECT_SOURCE_DIR}/include )############################################################ # Create an executable ############################################################# Add an executable with the above sources add_executable(cmake_examples_inst_binsrc/main.cpp )# link the new hello_library target with the hello_binary target target_link_libraries( cmake_examples_inst_binPRIVATE cmake_examples_inst )############################################################ # Install ############################################################# Binaries install (TARGETS cmake_examples_inst_binDESTINATION bin)# Library # Note: may not work on windows install (TARGETS cmake_examples_instLIBRARY DESTINATION lib)# Header files install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ DESTINATION include)# Config install (FILES cmake-examples.confDESTINATION etc) -
[cmake-examples.conf] - 示例配置文件
# Sample configuration file that could be installed -
[include/installing/Hello.h] - 要包含的標題文件
#ifndef __HELLO_H__ #define __HELLO_H__class Hello { public:void print(); };#endif -
[src/Hello.cpp] - 要編譯的源文件
#include <iostream>#include "installing/Hello.h"void Hello::print() {std::cout << "Hello Install!" << std::endl; } -
[src/main.cpp] - 主源文件
#include "installing/Hello.h"int main(int argc, char *argv[]) {Hello hi;hi.print();return 0; }
概念#
安裝#
CMake提供了添加make install目標的功能,以允許用戶安裝二進制文件、庫和其他文件。基本安裝位置由變量CMAKE_INSTALL_PREFIX控制,該變量可以使用ccmake或通過使用cmake .. -DCMAKE_INSTALL_PREFIX=/install/location調用cmake來設置。
安裝的文件由install()函數控制。
install (TARGETS cmake_examples_inst_binDESTINATION bin)將目標cmake_examples_inst_bin生成的二進制文件安裝到目標目錄${CMAKE_INSTALL_PREFIX}/bin中。
install (TARGETS cmake_examples_instLIBRARY DESTINATION lib)將目標cmake_examples_inst生成的共享庫安裝到目標目錄${CMAKE_INSTALL_PREFIX}/lib中。
install (TARGETS cmake_examples_instLIBRARY DESTINATION libRUNTIME DESTINATION bin) install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/DESTINATION include)將針對cmake_examples_inst庫進行開發的頭文件安裝到${CMAKE_INSTALL_PREFIX}/include目錄中。
install (FILES cmake-examples.confDESTINATION etc)將配置文件安裝到目標${CMAKE_INSTALL_PREFIX}/etc。
在運行make install之后,CMake會生成一個install_mark.txt文件,其中包含所有已安裝文件的詳細信息。
構建示例#
$ 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!注意事項#
覆蓋默認安裝位置#
如前所述,默認安裝位置是從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()此示例將默認安裝位置設置為你的構建目錄下。
目標文件夾#
如果你希望通過進行安裝來確認是否包含所有文件,則make install目標支持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卸載#
默認情況下,CMake不會添加make uninstall目標。有關如何生成卸載目標的詳細信息,請參閱此常見問題解答
要從本例中刪除文件的簡單方法,可以使用:
sudo xargs rm < install_manifest.txt總結
以上是生活随笔為你收集整理的CMake基础 第5节 安装项目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CMake基础 第4节 动态库
- 下一篇: CMake基础 第7节 编译标志