例如,我怎么知道我的可执行目标E是否依赖于我的库目标L?
让我们的图像E取决于L1和L2,但我不知道它们是否依赖于L.
target_link_libraries(E L1 L2)
我想在调用target_link_libraries之前从cmake本身获取列表,这样如果我检测到E依赖于两个不兼容的库,我可以做一些技巧.我玩了一些GetPrerequisites,但是这找到了对磁盘上现有库的依赖性,而不是正在构建的目标库.
谢谢
通常需要确保CMake构建项目在编译后最终位于某个位置,并且该add_custom_command(..POST_BUILD...)
命令是实现该目标的通用设计模式:
add_custom_command(
TARGET mytarget
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:mytarget> ${CMAKE_BINARY_DIR}/final_destination
)
Run Code Online (Sandbox Code Playgroud)
令人遗憾的是,当所讨论的目标位于相对于包含add_custom_command
调用的文件的子目录中时,该文件是通过add_subdirectory()
命令递归编译的。尝试这样做会导致以下错误消息:
CMake Warning (dev) at CMakeLists.txt:4 (add_custom_command):
Policy CMP0040 is not set: The target in the TARGET signature of
add_custom_command() must exist. Run "cmake --help-policy CMP0040" for
policy details. Use the cmake_policy command to set the policy and
suppress this warning.
TARGET 'mytarget' was not created in this directory.
This warning is for project developers. Use -Wno-dev to suppress it.
Run Code Online (Sandbox Code Playgroud)
在许多情况下,有一个简单的解决方法:只需确保该add_custom_command() …
cmake ×2