CMake:如何构建外部项目并包含其目标 [英] CMake: How to build external projects and include their targets
问题描述
我有一个项目A导出静态库作为目标:
install(TARGETS alib DESTINATION lib EXPORT project_a-目标)
install(EXPORT project_a-targets DESTINATION lib / alib)
使用项目A作为项目B的外部项目,并包括其建立的目标:
ExternalProject_Add(project_a
URL .. .project_a.tar.gz
PREFIX $ {CMAKE_CURRENT_BINARY_DIR} / project_a
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH =< INSTALL_DIR>
)
include($ {CMAKE_CURRENT_BINARY_DIR} /lib/project_a/project_a-targets.cmake)
问题是包含文件不存在但是当运行项目B的CMakeLists时。
有没有办法使包依赖于正在构建的外部项目?
正如你所说,高度灵活的
a href =http://www.cmake.org/cmake/help/v2.8.10/cmake.html#module%3aExternalProject> ExternalProject
模块在构建时运行它的命令,所以你不能直接使用项目A的导入文件,因为只有安装了项目A后才创建它。 如果你想包含
项目A的导入文件,您将在 之前手动安装Project A,然后再调用Project B的CMakeLists.txt - 就像任何其他第三方依赖以此方式添加或通过 find_file
/ find_library
/ find_package
。
如果你想使用 ExternalProject_Add
,你需要在你的CMakeLists.txt:
ExternalProject_Add(project_a
URL ... project_a.tar.gz
PREFIX $ {CMAKE_CURRENT_BINARY_DIR} / project_a
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH =< INSTALL_DIR>
)
include($ {CMAKE_CURRENT_BINARY_DIR} /lib/project_a/project_a-targets.cmake)
ExternalProject_Get_Property(project_a install_dir)
include_directories($ {install_dir} / include)
add_dependencies(project_b_exe project_a)
target_link_libraries(project_b_exe $ {install_dir} /lib/alib.lib)
I have a Project A that exports a static library as a target:
install(TARGETS alib DESTINATION lib EXPORT project_a-targets)
install(EXPORT project_a-targets DESTINATION lib/alib)
Now I want to use Project A as an external project from Project B and include its built targets:
ExternalProject_Add(project_a
URL ...project_a.tar.gz
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/project_a
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
)
include(${CMAKE_CURRENT_BINARY_DIR}/lib/project_a/project_a-targets.cmake)
The problem is that the include file does not exist yet when CMakeLists of Project B is run.
Is there a way to make the include dependent on the external project being built?
I think you're mixing up two different paradigms here.
As you noted, the highly flexible ExternalProject
module runs its commands at build time, so you can't make direct use of Project A's import file since it's only created once Project A has been installed.
If you want to include
Project A's import file, you'll have to install Project A manually before invoking Project B's CMakeLists.txt - just like any other third-party dependency added this way or via find_file
/ find_library
/ find_package
.
If you want to make use of ExternalProject_Add
, you'll need to add something like the following to your CMakeLists.txt:
ExternalProject_Add(project_a
URL ...project_a.tar.gz
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/project_a
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
)
include(${CMAKE_CURRENT_BINARY_DIR}/lib/project_a/project_a-targets.cmake)
ExternalProject_Get_Property(project_a install_dir)
include_directories(${install_dir}/include)
add_dependencies(project_b_exe project_a)
target_link_libraries(project_b_exe ${install_dir}/lib/alib.lib)
这篇关于CMake:如何构建外部项目并包含其目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!