首页 > 解决方案 > Why does CMake export() command defer the output file creation? Can it be avoided?

问题描述

We are including separate repositories into a common CMake superproject (which simply add_subdirectory() each repository folder, in the correct dependency order).

The different repositories all rely on export() to ouptut a XxxTargets.cmake file, such as:

# Assumes this repository provide the 'Alpha' target
install(TARGETS Alpha EXPORT AlphaTargets)

export(EXPORT $AlphaTargets
       FILE $AlphaTargets.cmake
       NAMESPACE myrepo::)

Yet, this superproject approach allowed us to observe that the export does not create the file directly. We could draw this conclusion because when the generated file is include() from another repository (added later, with a subsequent add_subdirectory()), the file does not exist yet.

标签: cmake

解决方案


  • 为什么会export()延迟文件创建?

因为您要求它为特定的导出集生成代码,直到配置步骤结束才完全定义。如果它立即生成它,那么export()and的行为install(EXPORT)将是令人烦恼和不一致的。

  • 更重要的是,有没有办法强制它在下一个之前实际创建文件add_subdirectory()

export(TARGETS)我所知,签名会立即运行。但我也想不出在使用add_subdirectory. 目标已经存在,因此无需导入它们!如果名称不同,请根据需要创建 ALIAS 目标。


推荐阅读