CMake 安装(子目录中的 TARGETS)

CMake install (TARGETS in subdirectories)(CMake 安装(子目录中的 TARGETS))

本文介绍了CMake 安装(子目录中的 TARGETS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下 CMakeLists.txt 文件:

add_subdirectory(execA)
add_subdirectory(libB)

install(TARGETS execA libB
        RUNTIME DESTINATION bin
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib)

我收到以下错误:

install TARGETS given target "execA" which does not exist in this
  directory

execAlibB 有自己的 CMakeList.txt 文件,也位于 project 目录下作为我运行的构建目录 cmake (cmake ..):

execA and libB have their own CMakeList.txt files and are located under project directory, as well as the build directory I'm running cmake (cmake ..):

project
  |------ CMakeList.txt (the one with the code)
  |----execA
  |      - .cpp, .hpp and CMakelist.txt
  |----libB
  |      - .cpp, .hpp and CMakelist.txt
  |---- lib
  |---- bin
  ---- build (where I´m commanding: $ cmake ..

我该如何解决这个错误?

How do I fix this error?

推荐答案

根据 this bugreport、install(TARGETS) 命令流只接受在同一目录中创建的目标.

According to this bugreport, install(TARGETS) command flow accepts only targets created within the same directory.

因此您需要将 add_library() 调用移动到顶级目录,或者将 install(TARGETS) 调用拆分为每个目标的调用,然后将每个调用将它们放到相应的子目录中.

So you need either move the add_library() call into the top-level directory, or split install(TARGETS) call into per-target ones, and move each of them into the corresponding subdirectory.

自 CMake 3.13 install(TARGETS) 即使使用在其他目录中创建的目标也可以工作.

Since CMake 3.13 install(TARGETS) can work even with targets created in other directories.

install(TARGETS) 可以安装在其他目录中创建的目标.当使用这样的跨目录安装规则时,从子目录运行 make install(或类似的)不能保证来自其他目录的目标是最新的.

install(TARGETS) can install targets that were created in other directories. When using such cross-directory install rules, running make install (or similar) from a subdirectory will not guarantee that targets from other directories are up-to-date.

这篇关于CMake 安装(子目录中的 TARGETS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:CMake 安装(子目录中的 TARGETS)

基础教程推荐