cmake:使用每个构建重新生成文件

时间:2017-03-14 12:49:01

标签: cmake

generated.h是由目前替换为touch的脚本创建的。如何在每次运行make时重新生成此文件?如下例所示调用rm会产生错误。

cmake_minimum_required(VERSION 3.5.1)
project(MyProject)

set_source_files_properties(generated.h PROPERTIES GENERATED TRUE)

add_executable(jr
    jr.cpp
    generated.h
    )

add_custom_command(
        OUTPUT generated.h
        COMMAND rm generated.h
        COMMAND touch generated.h
        )

1 个答案:

答案 0 :(得分:1)

而不是add_custom_command使用add_custom_target:每次运行构建时都会执行它:

add_custom_target(regenerate
    COMMAND rm -f generated.h # Remove file if it exists.
    COMMAND touch generated.h
)

# Force executable to be compiled after regeneration takes a place
add_dependencies(jr regenerate)

# You need to hint compiler about location of the header file generated.
include_directories(${CMAKE_CURRENT_BINARY_DIR})