将导入的库链接到 CMake ExternalProject
Posted
技术标签:
【中文标题】将导入的库链接到 CMake ExternalProject【英文标题】:Link imported libraries to CMake ExternalProject 【发布时间】:2017-05-02 18:03:10 【问题描述】:我正在尝试创建一个超级构建来编译依赖项(zlib、nifticlib 和 boost),然后根据所有这些库编译我的项目。
这是我的 CMakeLists:
cmake_minimum_required(VERSION 3.6)
project(CMakeTest)
#-----------------------------------------------------------------------------
# Build options
#-----------------------------------------------------------------------------
option(BUILD_MYPROJECT "Build MYPROJECT." ON)
#-----------------------------------------------------------------------------
# Git protocole option
#-----------------------------------------------------------------------------
option(USE_GIT_PROTOCOL "If behind a firewall turn this off to use http instead." OFF)
set(git_protocol "git")
if(NOT USE_GIT_PROTOCOL)
set(git_protocol "https")
endif()
#-----------------------------------------------------------------------------
# Enable and setup External project global properties
#-----------------------------------------------------------------------------
include(ExternalProject)
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
FORCE)
endif()
#-----------------------------------------------------------------------------
# Zlib
#-----------------------------------------------------------------------------
message(STATUS "Installing Zlib library.")
ExternalProject_Add(Zlib
SOURCE_DIR "$PROJECT_BINARY_DIR/deps/zlib"
BINARY_DIR "$PROJECT_BINARY_DIR/deps/zlib-build"
INSTALL_DIR "$PROJECT_BINARY_DIR/deps/zlib-install"
GIT_REPOSITORY "$git_protocol://github.com/madler/zlib.git"
GIT_TAG "50893291621658f355bc5b4d450a8d06a563053d"
CMAKE_ARGS
-DCMAKE_BUILD_TYPE:STRING=$CMAKE_BUILD_TYPE
-DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
-DCMAKE_BIN_DIR:PATH=<INSTALL_DIR>/bin
-DINSTALL_INC_DIR:PATH=<INSTALL_DIR>/include
-DINSTALL_LIB_DIR:PATH=<INSTALL_DIR>/lib
-DINSTALL_MAN_DIR:PATH=<INSTALL_DIR>/share/man
-DINSTALL_PKGCONFIG_DIR:PATH=<INSTALL_DIR>/share/pkgconfig)
if(WIN32)
set(ZLIB_LIB_BASE_NAME "zlibstatic")
set(ZLIB_LIB_NAME_RELEASE "$CMAKE_STATIC_LIBRARY_PREFIX$ZLIB_LIB_BASE_NAME$CMAKE_STATIC_LIBRARY_SUFFIX")
set(ZLIB_LIB_NAME_DEBUG "$CMAKE_STATIC_LIBRARY_PREFIX$ZLIB_LIB_BASE_NAMEd$CMAKE_STATIC_LIBRARY_SUFFIX")
elseif(UNIX)
set(ZLIB_LIB_BASE_NAME "z")
set(ZLIB_LIB_NAME_RELEASE "$CMAKE_STATIC_LIBRARY_PREFIX$ZLIB_LIB_BASE_NAME$CMAKE_STATIC_LIBRARY_SUFFIX")
set(ZLIB_LIB_NAME_DEBUG "$CMAKE_STATIC_LIBRARY_PREFIX$ZLIB_LIB_BASE_NAME$CMAKE_STATIC_LIBRARY_SUFFIX")
else()
# MacOSX
endif()
ExternalProject_Get_Property(Zlib install_dir)
set(ZLIB_LIBRARY_DIR $install_dir/lib)
set(ZLIB_INCLUDE_DIR $install_dir/include)
set(ZLIB_BINARY_DIR $install_dir/bin)
add_library(zlib STATIC IMPORTED)
set_target_properties(zlib PROPERTIES IMPORTED_LOCATION_DEBUG "$ZLIB_LIBRARY_DIR/$ZLIB_LIB_NAME_DEBUG")
set_target_properties(zlib PROPERTIES IMPORTED_LOCATION_RELEASE "$ZLIB_LIBRARY_DIR/$ZLIB_LIB_NAME_RELEASE")
#-----------------------------------------------------------------------------
# Niftilib
#-----------------------------------------------------------------------------
message(STATUS "Installing Nifti library.")
ExternalProject_Add(Nifticlib
SOURCE_DIR "$PROJECT_BINARY_DIR/deps/nifticlib"
BINARY_DIR "$PROJECT_BINARY_DIR/deps/nifticlib-build"
INSTALL_DIR "$PROJECT_BINARY_DIR/deps/nifticlib-install"
GIT_REPOSITORY "$git_protocol://gitlab.com/slckr/nifticlib.git"
GIT_TAG "e26a94e947c210104223f9f49737392c742c1c5b"
CMAKE_ARGS
-DCMAKE_BUILD_TYPE:STRING=$CMAKE_BUILD_TYPE
-DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
-DZLIB_INCLUDE_DIR:PATH=$ZLIB_INCLUDE_DIR
-DZLIB_LIBRARY_DEBUG:PATH=$ZLIB_LIBRARY_DIR/$ZLIB_LIB_NAME_DEBUG
-DZLIB_LIBRARY_RELEASE:PATH=$ZLIB_LIBRARY_DIR/$ZLIB_LIB_NAME_RELEASE
DEPENDS Zlib)
set(NIFTIIO_LIB_BASE_NAME "niftiio")
set(NIFTIIO_LIB_NAME "$CMAKE_STATIC_LIBRARY_PREFIX$NIFTIIO_LIB_BASE_NAME$CMAKE_STATIC_LIBRARY_SUFFIX")
set(NIFTICDF_LIB_BASE_NAME "nifticdf")
set(NIFTICDF_LIB_NAME "$CMAKE_STATIC_LIBRARY_PREFIX$NIFTICDF_LIB_BASE_NAME$CMAKE_STATIC_LIBRARY_SUFFIX")
set(ZNZ_LIB_BASE_NAME "znz")
set(ZNZ_LIB_NAME "$CMAKE_STATIC_LIBRARY_PREFIX$ZNZ_LIB_BASE_NAME$CMAKE_STATIC_LIBRARY_SUFFIX")
ExternalProject_Get_Property(Nifticlib install_dir)
set(NIFTI_LIBRARY_DIR $install_dir/lib)
set(NIFTI_INCLUDE_DIR $install_dir/include/nifti)
set(NIFTI_BINARY_DIR $install_dir/bin)
add_library(niftiio STATIC IMPORTED)
set_target_properties(niftiio PROPERTIES IMPORTED_LOCATION "$NIFTI_LIBRARY_DIR/$NIFTIIO_LIB_NAME")
add_library(nifticdf STATIC IMPORTED)
set_target_properties(nifticdf PROPERTIES IMPORTED_LOCATION "$NIFTI_LIBRARY_DIR/$NIFTICDF_LIB_NAME")
add_library(znz STATIC IMPORTED)
set_target_properties(znz PROPERTIES IMPORTED_LOCATION "$NIFTI_LIBRARY_DIR/$ZNZ_LIB_NAME")
#-----------------------------------------------------------------------------
# Boost
#-----------------------------------------------------------------------------
message(STATUS "Installing Boost library.")
set(BOOST_BOOTSTRAP_COMMAND)
if(WIN32)
set(BOOST_BOOTSTRAP_COMMAND bootstrap.bat)
set(BOOST_B2_COMMAND b2.exe)
elseif(UNIX )
set(BOOST_BOOTSTRAP_COMMAND ./bootstrap.sh)
set(BOOST_B2_COMMAND ./b2)
else()
# MacOSX
set(BOOST_BOOTSTRAP_COMMAND ./bootstrap.sh)
set(BOOST_B2_COMMAND ./b2)
endif()
set(BOOST_BUILD_TYPE variant=release)
if ($CMAKE_BUILD_TYPE MATCHES Debug)
set(BOOST_BUILD_TYPE variant=debug)
endif($CMAKE_BUILD_TYPE MATCHES Debug)
set(BOOST_INSTALL_DIR $PROJECT_BINARY_DIR/deps/boost-install)
ExternalProject_Add(boost
SOURCE_DIR "$PROJECT_BINARY_DIR/deps/boost"
BUILD_IN_SOURCE 1
GIT_REPOSITORY "$git_protocol://github.com/boostorg/boost"
GIT_TAG "5ec478a570bdc71c5d4854e7165a8b3f4fa82ad9"
CONFIGURE_COMMAND $BOOST_BOOTSTRAP_COMMAND
BUILD_COMMAND $BOOST_B2_COMMAND headers COMMAND $BOOST_B2_COMMAND install
link=static
$BOOST_BUILD_TYPE
--prefix=$BOOST_INSTALL_DIR
--with-filesystem
--with-program_options
--with-system
--with-thread
-j8
INSTALL_COMMAND ""
)
if(WIN32)
set(BOOST_LIBRARY_DIR $BOOST_INSTALL_DIR/lib/boost)
set(BOOST_INCLUDE_DIR $BOOST_INSTALL_DIR/include/boost-1_65)
else()
set(BOOST_LIBRARY_DIR $BOOST_INSTALL_DIR/lib/boost)
set(BOOST_INCLUDE_DIR $BOOST_INSTALL_DIR/include)
endif()
set(BOOST_SUBMODULES system;filesystem;program_options;thread)
#-----------------------------------------------------------------------------
# MyProject
#-----------------------------------------------------------------------------
message(STATUS "Installing MyProject library.")
if(BUILD_MYPROJECT)
set(MYPROJECT_LIBS zlib;znz;nifticdf;niftiio)
ExternalProject_Add(MYPROJECT
SOURCE_DIR "$PROJECT_BINARY_DIR/MyProject"
BINARY_DIR "$PROJECT_BINARY_DIR/MyProject-build"
INSTALL_DIR "$PROJECT_BINARY_DIR/MyProject-install"
GIT_REPOSITORY "$git_protocol://gitlab.com/slckr/MyProject.git"
GIT_TAG "origin/multithread2"
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
-DCMAKE_BUILD_TYPE:STRING=$CMAKE_BUILD_TYPE
-DZLIB_INC_DIR:PATH=$ZLIB_INCLUDE_DIR
-DZLIB_LIB_DIR:PATH=$ZLIB_LIBRARY_DIR
-DNIFTI_INC_DIR:PATH=$NIFTI_INCLUDE_DIR
-DNIFTI_LIB_DIR:PATH=$NIFTI_LIBRARY_DIR
-DBOOST_INC_DIR:PATH=$BOOST_INCLUDE_DIR
-DBOOST_LIB_DIR:PATH=$BOOST_LIBRARY_DIR
-DBOOST_SUBMODULES:STRING=$BOOST_SUBMODULES
-DMYPROJECT_LIBS:STRING=$MYPROJECT_LIBS
DEPENDS Nifticlib boost)
endif()
问题是当我尝试将 MyProject 链接到 nifticlib 或 boost 时,因为库名称因平台而异(我在导入库时处理)链接失败。
我通过库目录和 MyProject CMakeLists,我这样做:
include_directories($ZLIB_INC_DIR $NIFTI_INC_DIR $BOOST_INC_DIR)
link_directories($ZLIB_LIB_DIR $NIFTI_LIB_DIR $BOOST_LIB_DIR)
target_link_libraries(MyProject zlib znz niftiio nifticdf)
但 target_link_libraries 失败,因为未找到 zlib(但我将它导入到我的 superbuild 中)。如何告诉 MyProject 使用我的 superbuild 的导入库?
谢谢。
【问题讨论】:
【参考方案1】:我认为这个https://crascit.com/2015/07/25/cmake-gtest/ 可以帮助你。
链接中方法的要点是在配置时构建外部项目。这会将外部项目完全集成到您的构建中并提供对目标的访问权限。
不幸的是,该示例使用 gtest 来演示使用,但可以将其用于其他依赖项。
【讨论】:
是的,我看到了这种方法,但是,它看起来更像是一种 hack,而不是一种干净的方法。我认为在最坏的情况下,我可以将库名称传递到我的项目的 CMAKE_ARGS 中,但仍然不是很干净。我不明白为什么 ExternalProject_Add 可以有依赖项,但只用于构建顺序,并且不将依赖项的所有变量也传递给外部项目。以上是关于将导入的库链接到 CMake ExternalProject的主要内容,如果未能解决你的问题,请参考以下文章
Cmake:将子目录链接模式覆盖为 LINK_PRIVATE