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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM