为什么CMake无法检测到对生成文件的依赖性? [英] Why doesn't CMake detect dependency on my generated file?

查看:125
本文介绍了为什么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 <如上所述,/ code>根本不会创建 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无法检测到对生成文件的依赖性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆