CMake目标取决于安装的目标 [英] CMake target depends on installed target

查看:128
本文介绍了CMake目标取决于安装的目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索过此内容,但也许我使用了错误的措词;我希望在安装另一个目标之后再构建一个CMake目标。

I have searched for this, but maybe I am using wrong wording; I want a CMake target to be built after another target is installed.

通过一个具体的示例,我希望我的测试包含库的版本并与之链接,其目录结构类似于实际安装。
目录结构:

With a concrete example, I want my tests to include from and link with a version of the library, whose directory structure resembles actual install. Directory structure:

project
  lib
    first_library
      header1.hpp
      source1.cpp # this includes "first_library/header1.hpp"
    second_library
      header2.hpp
      source2.cpp # likewise, #include "second_library/header2.hpp"
  tests
    lib1_tests
      test1.cpp # this must include "first_library/header1.hpp"
    lib2_tests
      test2.cpp # likewise, #include "second_library/header2.hpp"

这些都不适合我。在lib目录的CMakeLists.txt中,我有:

Neither of these worked for me. In the CMakeLists.txt in lib directory, I have:

add_library(lib1 STATIC ${lib1_SOURCES} ${lib1_HEADERS})
set_property(TARGET lib1 APPEND
    PROPERTY INCLUDE_DIRECTORIES "${CMAKE_CURRENT_LIST_DIR}/../")
install(TARGETS lib1 EXPORT lib1
    ARCHIVE DESTINATION lib
    INCLUDES DESTINATION include)
install(FILES ${lib1_HEADERS}
    DESTINATION include/my_lib_collection/first_library)

并且测试有

add_executable(tests "${TEST_SOURCES}")
add_dependencies(tests lib1)
add_dependencies(tests lib2)
target_link_libraries(tests ${GTEST_BOTH_LIBRARIES}
    lib1 lib2)
target_include_directories(tests
    INTERFACE $<INSTALL_INTERFACE:INTERFACE_INCLUDE_DIRECTORIES:include/my_lib_collection>)
set_property(TARGET tests APPEND
    PROPERTY INCLUDE_DIRECTORIES ${GTEST_INCLUDE_DIRS})

最后,什么一世want是一个与安装状态兼容的目录结构,并且能够在构建测试时使用它们。

Ultimately, what I want is a directory structure that is compatible with the installed state, and being able to use these while building tests.

谢谢!

推荐答案

我遇到了您现在面临的相同问题。
不能说我找到了一个教科书解决方案,但是我设法解决了这个问题

I was facing the same problem you are facing now. can't say I found a text-book solution, but I did manage to work around it

我有多个带有CmakeList.txt文件的子目录对于每个它们。

I had multiple sub directories with a CmakeList.txt file for each of them.

有一个分支,所有其他分支都依赖于它的安装
我所做的是 add_custom_target (而不是 add_subdirectory )和该自定义目标中的命令,其中用于构建子目录的命令行

there was one branch that all the rest of the branched depended on it being installed what I did was using add_custom_target (instead of add_subdirectory) and the commands in that custom target where the command lines for building the subdirectory

这篇关于CMake目标取决于安装的目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆