我正在尝试使用自定义命令生成标头。每次重建时都应更新标头,以便包含它的源文件也将被重建。 (实际命令是一个脚本,但这里是一个简化版本。)
这是我的项目:
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
#include <version.h>
int main()
{
return 0;
}
现在的问题是CMakeList.txt
,如上所述,不会导致version.h
正在被创造。只有在我切换之后add_custom_target
to add_custom_command
确实如此。但是,如果我以某种方式更改文件,下一步make
不重建项目。
看起来 CMake 无法识别test.c
依赖于取决于version.h
,尽管它明确地#include
它。我在这里做错了什么?
Change:
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)
Into:
add_custom_command(OUTPUT "${VERSION_H_PATH}" COMMAND ${CMAKE_COMMAND} -E touch "${VERSION_H_PATH}") #More reliable touch, use cmake itself to touch the file
add_custom_target(generate_version_h DEPENDS "${VERSION_H_PATH}")
add_executable(myprog ${test_SOURCES})
add_dependencies(myprog generate_version_h)
See:
-
CMake命令行工具模式 https://cmake.org/cmake/help/latest/manual/cmake.1.html#command-line-tool-mode.
-
添加自定义目标 https://cmake.org/cmake/help/latest/command/add_custom_target.html:
DEPENDS:
使用同一目录(CMakeLists.txt 文件)中的 add_custom_command() 命令调用创建的自定义命令的参考文件和输出。当目标构建时,它们将被更新。
-
添加依赖项 https://cmake.org/cmake/help/latest/command/add_dependencies.html:
使顶级目标依赖于其他顶级目标,以确保它们先于构建。顶级目标是由 add_executable()、add_library() 或 add_executable() 之一创建的目标添加自定义目标()命令(但不是由 CMake 生成的目标,如安装)。
顺便说一句,我不知道你的具体情况,但你可以考虑使用配置文件 https://cmake.org/cmake/help/latest/command/configure_file.html生成您的标头。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)