cmake add_custom_command 不起作用

Sri*_*ant 5 cmake add-custom-command

我正在尝试gperfcmake文件运行。

我在CMakeLists.txt下面创建了一个非常小的。

当我运行它时

$ cmake .
$ make 
Run Code Online (Sandbox Code Playgroud)

它不会创建example.hpp文件

下面可能有什么问题CMakeLists.txt

cmake_minimum_required( VERSION 2.6 )

function(gperf_generate_new source target)

        add_custom_target(${target} echo "Creating ${target}")

        add_custom_command(
                SOURCE ${source}
                TARGET ${target}
                COMMAND gperf -L c++ ${source} > ${target}
                OUTPUTS ${target}
                DEPENDS ${source}
                )

endfunction()

gperf_generate_new(command_options.new.gperf example.hpp)
Run Code Online (Sandbox Code Playgroud)

Tsy*_*rev 3

由源文件生成器(如)生成的文件gpref很少需要独立使用。相反,这些源文件通常用于在项目内创建可执行文件或库。

因此,在 CMake 中使用源文件生成器的标准模式如下所示:

# Call add_custom_command() with appropriate arguments for generate output file
# Note, that *gperf* will work in the build tree,
# so for file in the source tree full path should be used.
function(gperf_generate_new input output)
    add_custom_command(
        OUTPUT ${output}
        COMMAND gperf -L c++ ${input} > ${output}
        DEPENDS ${input}
        COMMENT "Generate ${output}" # Just for nice message during build
    )
endfunction()

# Generate *example.hpp* file ...
gperf_generate_new(${CMAKE_CURRENT_SOURCE_DIR}/command_options.new.gperf example.hpp)

# ... for use it in executable
add_executable(my_program ${CMAKE_CURRENT_BINARY_DIR}/example.hpp <other sources>)
Run Code Online (Sandbox Code Playgroud)

如果您只想测试是否example.hpp正在生成,而不是add_executable()使用

add_custom_target(my_target
    ALL # Force target to be built with default build target.
    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/example.hpp
)
Run Code Online (Sandbox Code Playgroud)

add_custom_command请注意,和之间的链接add_custom_target是使用相应的OUTPUTDEPENDS选项中的相同文件名来表达的。通过这种链接顺序,这些命令的顺序并不重要(但是这两个命令应该从同一CMakeLists.txt脚本调用)。