gpt4 book ai didi

CMake:如何将 INTERFACE_INCLUDE_DIRECTORIES 与 ExternalProject 一起使用?

转载 作者:行者123 更新时间:2023-12-03 15:07:16 28 4
gpt4 key购买 nike

我正在尝试使用 ExternalProject_Add 将外部项目作为库添加到我的项目中:

ExternalProject_Add(
xgboost
GIT_REPOSITORY https://github.com/dmlc/xgboost.git
GIT_TAG v0.60
PREFIX ${CMAKE_CURRENT_BINARY_DIR}
INSTALL_COMMAND ""
)

另外,我正在定义库目标并将外部项目添加为依赖项:
set(XGBOOST_PREFIX_PATH "${CMAKE_CURRENT_BINARY_DIR}/src")
set(XGBOOST_BINARY_PATH "${XGBOOST_PREFIX_PATH}/xgboost-build")
set(XGBOOST_BINARY_INCLUDE "${XGBOOST_PREFIX_PATH}/xgboost/include;${XGBOOST_PREFIX_PATH}/xgboost/dmlc-core/include;${XGBOOST_PREFIX_PATH}/xgboost/rabit/include")
add_library(libxgboost IMPORTED STATIC GLOBAL)
add_dependencies(libxgboost xgboost)

set_target_properties(libxgboost PROPERTIES
"IMPORTED_LOCATION" "${XGBOOST_BINARY_PATH}/liblibxgboost.dylib"
"IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}"
"INTERFACE_INCLUDE_DIRECTORIES" "${XGBOOST_BINARY_INCLUDE}"
)

在另一个 CMakeLists.txt 中:
add_library(somelib STATIC SomeLib.cpp)
target_include_directories(somelib PUBLIC libxgboost)
target_link_libraries(somelib libxgboost)

问题是 cmake INTERFACE_INCLUDE_DIRECTORIES 不允许导出不存在的包含目录。

有没有其他方法可以让所有依赖 libxgboost 的目标自动包含头文件?

更新:

错误信息:
CMake Error in somelib/CMakeLists.txt:
Imported target "libxgboost" includes non-existent path

"build/xgboost/src/xgboost/include"

in its INTERFACE_INCLUDE_DIRECTORIES. Possible reasons include:

* The path was deleted, renamed, or moved to another location.

* An install or uninstall procedure did not complete successfully.

* The installation package was faulty and references files it does not
provide.

最佳答案

我会发布最终的 CMakeLists.txt将 xgboost 包含到您的项目中,它可能对某人有用,解决上述问题的方法是在 cmake 期间创建目录。配置阶段(注意:我使用 OSX 来构建它,所以你需要为 GNU/Linux 使用 liblibxgboost.so 而不是 liblibxgboost.dylib ):

include(ExternalProject)

ExternalProject_Add(
xgboost
GIT_REPOSITORY https://github.com/dmlc/xgboost.git
GIT_TAG v0.60
PREFIX ${CMAKE_CURRENT_BINARY_DIR}
INSTALL_COMMAND ""
)

set(XGBOOST_PREFIX_PATH "${CMAKE_CURRENT_BINARY_DIR}/src")
set(XGBOOST_BINARY_PATH "${XGBOOST_PREFIX_PATH}/xgboost-build")

set(XGBOOST_LIB_INCLUDE "${XGBOOST_PREFIX_PATH}/xgboost/include")
set(DMLC_LIB_INCLUDE "${XGBOOST_PREFIX_PATH}/xgboost/dmlc-core/include")
set(RABIT_LIB_INCLUDE "${XGBOOST_PREFIX_PATH}/xgboost/rabit/include")
set(XGBOOST_BINARY_INCLUDE "${XGBOOST_LIB_INCLUDE};${DMLC_LIB_INCLUDE};${RABIT_LIB_INCLUDE}")

# Hack to make it work, otherwise INTERFACE_INCLUDE_DIRECTORIES will not be propagated
file(MAKE_DIRECTORY ${XGBOOST_LIB_INCLUDE})
file(MAKE_DIRECTORY ${DMLC_LIB_INCLUDE})
file(MAKE_DIRECTORY ${RABIT_LIB_INCLUDE})

add_library(libxgboost IMPORTED STATIC GLOBAL)
add_dependencies(libxgboost xgboost)

set_target_properties(libxgboost PROPERTIES
"IMPORTED_LOCATION" "${XGBOOST_BINARY_PATH}/liblibxgboost.dylib"
"INTERFACE_INCLUDE_DIRECTORIES" "${XGBOOST_BINARY_INCLUDE}"
)

关于CMake:如何将 INTERFACE_INCLUDE_DIRECTORIES 与 ExternalProject 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45516209/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com