CMake中与export()相关的命令

(注:红色字体是标题,粉色是需要特别需要注意的地方)

总的来说,export()命令想要做的事情可以用一句话概括:Export targets from the build tree for use by outside projects.

其具体的用法有以下三个:

one:

export(EXPORT <export-name> [NAMESPACE <namespace>] [FILE <filename>])

Create a file <filename> that may be included by outside projects to import targets from the current project’s build tree.This is useful during cross-compiling to build utility executables that can run on the host platform in one project and then import them into another project being compiled for the target platform. If the NAMESPACE option is given the <namespace> string will be prepended to all target names written to the file.

Target installations are associated with the export <export-name> using the EXPORT option of the install(TARGETS) command.

eg:

install(TARGETS ${LIBRARY_NAME}
EXPORT ${PROJECT_NAME}Targets
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
)

The file created by this command is specific to the build tree and should never be installed. See the install(EXPORT) command to export targets from an installation tree.

eg:

install(
EXPORT ${PROJECT_NAME}Targets DESTINATION ${CMAKECONFIG_INSTALL_DIR}
)

The properties set on the generated IMPORTED targets will have the same values as the final values of the input TARGETS.

two:

export(TARGETS [target1 [target2 [...]]] [NAMESPACE <namespace>]
[APPEND] FILE <filename> [EXPORT_LINK_INTERFACE_LIBRARIES])

This signature is similar to the EXPORT signature, but targets are listed explicitly rather than specified as an export-name. If the APPEND option is given the generated code will be appended to the file instead of overwriting it.If a library target is included in the export but a target to which it links is not included the behavior is unspecified.

eg:

Pangolin的cmake工程文件中的一个CMakeLists.txt文件有一句命令为:

export( TARGETS ${LIBRARY_NAME}
        APPEND FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake" )

执行完后,build文件夹中会生成一个名为PangolinTargets.cmake的文件,主要内容为Pangolin库所链接的其它库的绝对路径,以及pangolin库在build树中的位置。这样一来,外部工程直接导入build树中的库文件就变的十分方便。

# Create imported target pangolin
add_library(pangolin SHARED IMPORTED) set_target_properties(pangolin PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "/usr/include;/usr/include;/usr/include;/usr/include/eigen3;/home/lzb/classic_pure_lib/Pangolin/include;/home/lzb/classic_pure_lib/Pangolin/build/src/include"
INTERFACE_LINK_LIBRARIES "/usr/lib/x86_64-linux-gnu/libGLU.so;/usr/lib/x86_64-linux-gnu/libGL.so;/usr/lib/x86_64-linux-gnu/libGLEW.so;wayland-egl;wayland-client;wayland-cursor;xkbcommon;EGL;/usr/lib/x86_64-linux-gnu/libSM.so;/usr/lib/x86_64-linux-gnu/libICE.so;/usr/lib/x86_64-linux-gnu/libX11.so;/usr/lib/x86_64-linux-gnu/libXext.so;rt;pthread;/usr/lib/libOpenNI.so;/usr/lib/x86_64-linux-gnu/libpng.so;/usr/lib/x86_64-linux-gnu/libz.so;/usr/lib/x86_64-linux-gnu/libjpeg.so;/usr/lib/x86_64-linux-gnu/libtiff.so;/usr/lib/x86_64-linux-gnu/liblz4.so"
) # Import target "pangolin" for configuration "Release"
set_property(TARGET pangolin APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(pangolin PROPERTIES
IMPORTED_LOCATION_RELEASE "/home/lzb/classic_pure_lib/Pangolin/build/src/libpangolin.so"
IMPORTED_SONAME_RELEASE "libpangolin.so"
)

eg:

eigen3中CMakeLists.txt中有这么一句命令:

export (TARGETS eigen    NAMESPACE Eigen3::    FILE Eigen3Targets.cmake)

执行之后,build文件夹中会生成一个名为Eigen3Targets.cmake的文件:

# Create imported target Eigen3::Eigen
add_library(Eigen3::Eigen INTERFACE IMPORTED) set_target_properties(Eigen3::Eigen PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "/home/lzb/classic_lib/eigen3"
)

Three:

export(PACKAGE <name>)

Store the current build directory in the CMake user package registry for package <name>. The find_package command may consider the directory while searching for package <name>. This helps dependent projects find and use a package from the current project’s build tree without help from the user. Note that the entry in the package registry that this command creates works only in conjunction with a package configuration file (<name>Config.cmake) that works with the build tree. In some cases, for example for packaging and for system wide installations, it is not desirable to write the user package registry. If the CMAKE_EXPORT_NO_PACKAGE_REGISTRY variable is enabled, the export(PACKAGE) command will do nothing.

eg:

export(PACKAGE Ceres) #这句话会在记录PACKAGEConfig.cmake的位置PACKAGE_DIR;

在项目的使用其它库,CMakeLists.txt中可以使用find_package(PROJECT_NAME) 命令即可以得到<PROJECT_NAME>_DIR的值,然后会自动将该路径下的<PROJECT_NAME>Config.cmake文件内容导入到项目中。

list( APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules )
find_package( Ceres REQUIRED ) #find_package()会先在指定的CMAKE_MOUDLE_PATH路径中寻找FindCeres.cmake文件;如果没有找到,那就在Ceres_DIR路径下寻找CeresConfig.cmake文件;

我们可以在find_package()之前指定Ceres_DIR的值来灵活调用:

set(Ceres_DIR "/home/lzb/classic_lib/Ceres/lib/cmake/Ceres/")

关于<PROJECT_NAME>Config.cmake

cmake项目中一般会提供<PROJECT_NAME>Config.cmake.in文件,在CMakeLists.txt中一般会将根据这个文件来生成两个在不同位置的<PROJECT_NAME>Config.cmake文件。

第一个是将要放在在build树中的<PROJECT_NAME>Config.cmake文件:

configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake @ONLY IMMEDIATE )

第二个是将要放在install树中的<PROJECT_NAME>Config.cmake文件:

configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${PROJECT_NAME}Config.cmake @ONLY )
... install(
    FILES "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${PROJECT_NAME}Config.cmake"
    DESTINATION ${CMAKECONFIG_INSTALL_DIR}
)

eg:下面是Pangolin工程中的<PROJECT_NAME>Config.cmake.in文件和生成的build树下的<PROJECT_NAME>Config.cmake文件,install树下的<PROJECT_NAME>Config.cmake文件。

#<PROJECT_NAME>Config.cmake.in
# Compute paths
get_filename_component( PROJECT_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH )
SET( Pangolin_INCLUDE_DIRS "@EXPORT_LIB_INC_DIR@;@USER_INC@" )
SET( Pangolin_INCLUDE_DIR "@EXPORT_LIB_INC_DIR@;@USER_INC@" ) # Library dependencies (contains definitions for IMPORTED targets)
if( NOT TARGET @LIBRARY_NAME@ AND NOT @PROJECT_NAME@_BINARY_DIR )
include( "${PROJECT_CMAKE_DIR}/@PROJECT_NAME@Targets.cmake" )
@ExternConfig@
endif() SET( Pangolin_LIBRARIES @LIBRARY_NAME@ )
SET( Pangolin_LIBRARY @LIBRARY_NAME@ )
SET( Pangolin_CMAKEMODULES @CMAKE_CURRENT_SOURCE_DIR@/../CMakeModules )
#build树下的<PROJECT_NAME>Config.cmake
# Compute paths
get_filename_component( PROJECT_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH )
SET( Pangolin_INCLUDE_DIRS "/home/lzb/classic_pure_lib/Pangolin/include;/home/lzb/classic_pure_lib/Pangolin/build/src/include;/usr/include;/usr/include;/usr/include;/usr/include/eigen3" )
SET( Pangolin_INCLUDE_DIR "/home/lzb/classic_pure_lib/Pangolin/include;/home/lzb/classic_pure_lib/Pangolin/build/src/include;/usr/include;/usr/include;/usr/include;/usr/include/eigen3" ) # Library dependencies (contains definitions for IMPORTED targets)
if( NOT TARGET pangolin AND NOT Pangolin_BINARY_DIR )
include( "${PROJECT_CMAKE_DIR}/PangolinTargets.cmake" ) endif() SET( Pangolin_LIBRARIES pangolin )
SET( Pangolin_LIBRARY pangolin )
SET( Pangolin_CMAKEMODULES /home/lzb/classic_pure_lib/Pangolin/src/../CMakeModules )
#install树下的<PROJECT_NAME>Config.cmake文件
# Compute paths
get_filename_component( PROJECT_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH )
SET( Pangolin_INCLUDE_DIRS "${PROJECT_CMAKE_DIR}/../../../include;/usr/include;/usr/include;/usr/include;/usr/include/eigen3" )
SET( Pangolin_INCLUDE_DIR "${PROJECT_CMAKE_DIR}/../../../include;/usr/include;/usr/include;/usr/include;/usr/include/eigen3" ) # Library dependencies (contains definitions for IMPORTED targets)
if( NOT TARGET pangolin AND NOT Pangolin_BINARY_DIR )
include( "${PROJECT_CMAKE_DIR}/PangolinTargets.cmake" ) endif() SET( Pangolin_LIBRARIES pangolin )
SET( Pangolin_LIBRARY pangolin )
SET( Pangolin_CMAKEMODULES /home/lzb/classic_pure_lib/Pangolin/src/../CMakeModules )

从上面可以看出,<PROJECT_NAME>Config.cmake文件提供库的接口变量,TARGET里需要的头文件路径变量:Pangolin_INCLUDE_DIR ,TARGET本身: Pangolin_LIBRARIES ,等等。

CMake命令之export的更多相关文章

  1. CMake命令/函数汇总(翻译自官方手册)

    查看官方文档 cmake命令 选项 CMake变量 CMake命令汇总 / add_custom_command add_custom_target/add_definitions/add_depen ...

  2. CMake命令

    CMake手册详解,作者翻译的很详细,以下是自己进行的摘录: CMake80个命令(详细解释可以看here) CMD#1: add_custom_command为生成的构建系统添加一条自定义的构建规则 ...

  3. Zsh安装CMake补全脚本进行CMake命令补全

    最近在尝试使用Zsh,发现其补全命令的功能相当厉害.但对CMake命令的补全在默认的5.0.5中好像没有看到,网上找了下关于配置Zsh补全的文章也没有多少.     于是自己动手,发现在Zsh安装目录 ...

  4. cmake命令收集

    cmake中一些预定义变量 PROJECT_SOURCE_DIR 工程的根目录 PROJECT_BINARY_DIR 运行cmake命令的目录,通常是${PROJECT_SOURCE_DIR}/bui ...

  5. CMake命令行添加编译参数

    CMake命令行添加编译参数 学习自 coroserver 例程: https://github.com/windoze/coroserver coroserver 是一个应用 Boost.Asio ...

  6. 【DB2】文件导入导出常见命令总结 EXPORT IMPORT LOAD

    参考文献地址:https://blog.csdn.net/reaper1022/article/details/18601973 Db2 的数据迁移,最常用的就是导入导出功能,而导入导出的命令貌似简单 ...

  7. cmake命令 安装、用法简介

    前言 cmake是kitware公司以及一些开源开发者在开发几个工具套件(VTK)的过程中所产生的衍生品.后来经过发展,最终形成体系,在2001年成为一个独立的开放源代码项目.其官方网站是www.cm ...

  8. mysql配置文件夹错误:在安装mysql 5.6.19 时运行cmake命令是出现CMake Error: The source directory does not appear to contai

    在安装mysql 5.5.xx 时运行cmake命令是出现CMake Error: The source directory does not appear to contain CMakeLists ...

  9. CMake命令之install

    CMAKE_INSTALL_PREFIX Install directory used by install(). if make install is invoked or INSTALL is b ...

随机推荐

  1. Elasticsearch开启试用x-pack license

    一.Elasticsearch 6.7.2开启trial  x-pack license:x-pack的license试用期只有30天 1.ES6.7.2版本默认已经安装了x-pack插件,这里就没有 ...

  2. 云时代架构阅读笔记五——Java内存模型详解(一)

    什么是Java内存模型 Java虚拟机规范中试图定义一种Java内存模型(Java Memory Model,JMM)来屏蔽掉各种硬件和操作系统的访问差异,以实现让Java程序在各种平台下都能达到一致 ...

  3. Android Studio 移动虚拟机

    突然间发现C盘 空间占用量增加了很多,经过找寻原因之后发现是因为安装了虚拟机的原因:在Android Studio中安装运行虚拟机时,默认的安装路径一般都在C盘,对于我这种不喜欢C盘存储太满的人来说是 ...

  4. 开源DDD设计模式框架YMNNetCoreFrameWork第二篇-增加swagger,数据库迁移,增加权限模型

    1.框架去掉application层,把HOst作为application层 2.增加swagger插件 3.增加Asp.net  Identity身份验证 源代码地址:https://github. ...

  5. 十一、JavaScript之两种注释方法

    一.代码如下 二.运行效果如下

  6. 在各浏览器和各分辨率下如何让div内的table垂直水平居中?

    本文主要针对需要全屏显示的系统页面内,因为系统经常会用到表格table布局,偶尔的table需要显示在div的正中间,所以鄙人特此总结下div内table的万千姿态. <!DOCTYPE htm ...

  7. 想要写好Synthesis Essay,学会审题很重要

    很多留学生都不太愿意写synthesis essay,因为它的难度要比其他类型的essay要大得多.写一篇synthesis essay需要有能力消化相关的信息,通过语言组织之后再呈现.虽然这种写作技 ...

  8. Node.js NPM 介绍

    章节 Node.js NPM 介绍 Node.js NPM 作用 Node.js NPM 包(Package) Node.js NPM 管理包 Node.js NPM Package.json NPM ...

  9. Linux运维命令笔记一

     1.Centos 无netstat 命令 yum -y install net-toolnetstat -tunp  2.Centos防火墙 systemctl stop firewalld.ser ...

  10. 2016蓝桥杯省赛C/C++A组第九题 密码脱落

    题意: X星球的考古学家发现了一批古代留下来的密码. 这些密码是由A.B.C.D 四种植物的种子串成的序列. 仔细分析发现,这些密码串当初应该是前后对称的(也就是我们说的镜像串). 由于年代久远,其中 ...