cmake--安装

一, 目录结构

├── cmake-examples.conf
├── CMakeLists.txt
├── include
│          └── installing
│          └── Hello.h
└── src
         ├── Hello.cpp
         └── main.cpp

* link:CMakeLists.txt[] - Contains the CMake commands you wish to run
* link:cmake-examples.conf[] - An example configuration file
* link:include/installing/Hello.h[] - The header file to include
* link:src/Hello.cpp[] - A source file to compile
* link:src/main.cpp[] - The source file with main

二,cmake脚本

cmake_minimum_required(VERSION 3.5)

project(cmake_examples_install)

# 根据源码创建共享库

add_library(cmake_examples_inst SHARED src/Hello.cpp)

#添加头文件目录

target_include_directories(cmake_examples_inst
PUBLIC
${PROJECT_SOURCE_DIR}/include
)

# 创建可执行程序
add_executable(cmake_examples_inst_bin
src/main.cpp
)

# 链接动态库
target_link_libraries( cmake_examples_inst_bin
PRIVATE
cmake_examples_inst
)

# 安装bin文件到指定目录
install (TARGETS cmake_examples_inst_bin DESTINATION bin)

# 安装库文件到指定目录,在windows上可能有问题!!!
# install (TARGETS cmake_examples_inst LIBRARY DESTINATION lib)

# 安装头文件到指定目录
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ DESTINATION include)

# 安装配置文件到指定目录
install (FILES cmake-examples.conf DESTINATION etc)

三,扩展分析

cmake提供安装文件的功能,通过执行make install将会安装编译后的可执行程序,配置文件,库文件等。

安装目录的目录前缀由变量CMAKE_INSTALL_PREFIX设置。在执行cmake程序的时候可以通过执行

类似下面这样的命令来设置CMAKE_INSTALL_PREFIX。

cmake .. -DCMAKE_INSTALL_PREFIX=/install/location

install(TARGETS cmake_example LIBRARY DESTINATION bin) 展开为

install(TARGETS cmake_example LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)

其他以此类推。

posted @ 2019-04-14 01:13  流浪侠客  阅读(1566)  评论(0编辑  收藏  举报