为什么CMake无法检测到对生成文件的依赖性?

编程入门 行业动态 更新时间:2024-10-28 04:15:25
本文介绍了为什么CMake无法检测到对生成文件的依赖性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用自定义命令生成标头。标头应在每次重建时更新,以便包含该标头的源文件也将被重建。 (实际命令是一个脚本,但这是简化的版本。)

I'm trying to generate a header with a custom command. The header should be updated on each rebuild, so that the source file which includes it would also be rebuilt. (Actual command is a script, but here is a simplified version.)

这是我的项目:

CMakeLists.txt

CMakeLists.txt

cmake_minimum_required(VERSION 2.8) set(test_SOURCES test.c) include_directories("${CMAKE_BINARY_DIR}") set(VERSION_H_PATH "${CMAKE_BINARY_DIR}/version.h") message("VERSION_H_PATH: ${VERSION_H_PATH}") add_custom_command(OUTPUT "${VERSION_H_PATH}" COMMAND "touch" "${VERSION_H_PATH}") #add_custom_target(GENERATE COMMAND "touch" "${VERSION_H_PATH}") add_executable(myprog ${test_SOURCES}) add_dependencies(myprog GENERATE)

test.c

test.c

#include <version.h> int main() { return 0; }

现在的问题是 CMakeList.txt 根本不会创建 version.h 。只有从 add_custom_target 切换到 add_custom_command 后,它才起作用。但是然后,如果我以某种方式更改文件,则下一个 make 不会重建项目。

Now the problem is that the CMakeList.txt, as presented above, doesn't result in version.h being created at all. Only after I switch from add_custom_target to add_custom_command does it do. But then, if I change the file somehow, next make doesn't rebuild the project.

看起来像CMake无法识别 test.c 取决于 version.h ,尽管它确实明确显示了 #include 。我在这里做什么错了?

Looks like CMake doesn't recognize that test.c depends on version.h, although it does explicitly #include it. What am I doing wrong here?

推荐答案

好的,实际上这个问题从一开始就提出来了。我不应该像我那样简化生成命令。但是我似乎已经通过删除构建目录的内容并重新运行 cmake 解决了这些问题。由于某些原因,cmake难以理解对 CMakeLists.txt 的某些更改。因此,这是有效的 CMakeLists.txt ,并且没有太多简化的生成器命令:

OK, actually the question was badly formulated from the beginning. I shouldn't have simplified the generating command as much as I did. But the problems I had seem to have been solved by removing content of build directory and re-running cmake. For some reason some changes to CMakeLists.txt are not easy for cmake to understand. So here's the CMakeLists.txt which does work, and with not too much simplified generator command:

cmake_minimum_required(VERSION 2.8) set(test_SOURCES test.c) include_directories("${CMAKE_BINARY_DIR}") set(VERSION_H_PATH "${CMAKE_BINARY_DIR}/version.h") message("VERSION_H_PATH: ${VERSION_H_PATH}") add_custom_target(GENERATE COMMAND "bash" "-c" "[ -e ${VERSION_H_PATH} ] \\|\\| touch ${VERSION_H_PATH}") add_executable(myprog ${test_SOURCES}) add_dependencies(myprog GENERATE)

即使使用保留的 test 目标而不是 myprog 。

This appears to work even when using the reserved test target instead of myprog.

更多推荐

为什么CMake无法检测到对生成文件的依赖性?

本文发布于:2023-10-30 21:57:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544122.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:依赖性   检测到   文件   CMake

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!