CMAKE:循环..依赖项已删除



我有一个第三方(预构建(可执行文件(名为other(,位于我的项目源树的导入文件夹中。基本上,项目的结构如下:

.
├── CMakeLists.txt
├── imported
│   ├── libother.so
│   └── other
└── main.cpp

为了让我的应用程序正常运行,其他可执行文件必须复制到我的执行文件旁边,我用这个脚本实现了什么:

cmake_minimum_required(VERSION 3.21)
project(myproject)
add_executable(myexe main.cpp)
set(path_in ${CMAKE_SOURCE_DIR}/imported/other)
set(path_out ${CMAKE_BINARY_DIR}/other)
add_executable(other_i IMPORTED)
set_target_properties(other_i PROPERTIES IMPORTED_LOCATION ${path_in})
add_custom_command(OUTPUT ${path_out}
COMMAND ${CMAKE_COMMAND} -E copy ${path_in} ${path_out}
DEPENDS other_i
)
add_custom_target(other DEPENDS ${path_out})
add_dependencies(myexe other)

问题是,每当项目在Linux上构建时,我都会收到来自gmake:的奇怪消息

$ cmake -S . -B .build
...
$ cmake --build .build
gmake[2]: Circular CMakeFiles/other <- other dependency dropped.
gmake[2]: Circular other <- other dependency dropped.

虽然其他被复制得很好,但每当我重新发出构建命令时,它总是被复制(即使没有任何更改(。这是不可取的,最让我困扰的是,它与Windows上的MSVC以及Linux上的共享库完美配合。例如,这很好:

cmake_minimum_required(VERSION 3.21)
project(myproject)
add_executable(myexe main.cpp)
set(path_in ${CMAKE_SOURCE_DIR}/imported/libother.so)
set(path_out ${CMAKE_BINARY_DIR}/libother.so)
add_library(other_i SHARED IMPORTED)
set_target_properties(other_i PROPERTIES IMPORTED_LOCATION ${path_in})
add_custom_command(OUTPUT ${path_out}
COMMAND ${CMAKE_COMMAND} -E copy ${path_in} ${path_out}
DEPENDS other_i
)
add_custom_target(other DEPENDS ${path_out})
add_dependencies(myexe other)

我对发生的事情束手无策。是CMAKE的错误还是我遗漏了什么?如有任何见解,我们将不胜感激。

PS:请不要建议在主目标上使用POST_BUILD,因为这种方法有自己的缺点。

add_custom_target(other DEPENDS ${path_out})

从CCD_ 2创建CCD_。但从Make的角度来看,它们是相同的东西:都指构建目录下的文件other(Makefile所在的位置(。正因为如此,你才会收到消息

gmake[2]: Circular other <- other dependency dropped.

问题的核心是,与CMake不同,Make没有将目标(纯名称(和文件分开(位于磁盘上(。从Make的视图来看,所有目标(甚至是.PHONY目标(都是文件。

为目标other使用另一个名称(例如other_exe(将消除该问题。

这似乎是CMake中的一个错误!我是这样处理的:

cmake_minimum_required(VERSION 3.21)
project(myproject)
set(path_in "${CMAKE_CURRENT_SOURCE_DIR}/imported/other")
set(path_out "${CMAKE_CURRENT_BINARY_DIR}/other")
add_custom_command(
OUTPUT "${path_out}"
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${path_in}" "${path_out}"
DEPENDS "${path_in}"
)
add_custom_target(other_update DEPENDS "${path_out}")
add_executable(myproject::other IMPORTED)
set_target_properties(myproject::other PROPERTIES IMPORTED_LOCATION "${path_out}")
add_dependencies(myproject::other other_update)
add_executable(myexe main.cpp)
add_dependencies(myexe myproject::other)

在指定依赖项时还有一些其他问题,所以我清理了这些问题。即便如此,我观察到,将可执行目标(即使导入(命名为与输出文件相同的名称会引发错误。您应该向CMake上游打开问题。

通常,利用CMake始终将包含::的名称视为目标名称这一事实是一个好主意。

相关内容

最新更新