CMake命令之export

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 ,等等。

 

posted @ 2019-07-15 00:36  心田居士  阅读(10658)  评论(0编辑  收藏  举报