cmake add_custom_command 不起作用

标签 cmake add-custom-command

我正在尝试运行 gperf来自 cmake文件。

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

当我运行它时

$ cmake .
$ make 

它不会创建 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)

最佳答案

由源文件生成器(如 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>)

如果只想测试是否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
)

请注意,add_custom_command 之间的链接和 add_custom_target在它们的 OUTPUT 中使用相同的文件名表示和 DEPENDS相应的选项。这些命令的链接顺序是无关紧要的(但应该从同一个 CMakeLists.txt 脚本中调用这两个命令)。

关于cmake add_custom_command 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33092108/

相关文章:

visual-studio - 如何在 Visual Studio 中执行自定义文件特定命令/任务?

CMake add_custom_command 和 target_link_libraries

Cmake:当位置不存在时,使用 add_custom_command 将二进制文件复制到特定位置失败

android - 在不使用 qmake 的情况下在 Android 的 Qt 应用程序上使用自定义 AndroidManifest?

cmake - cmake 如何找到我的 llvm cmake 配置?

cmake - 如何在 CMake 的 add_custom_command 中添加多个注释?

cmake - 如何从目标中获取包含目录以在 add_custom_target 中使用?

c++ - 在 CMake 中使用预编译的头文件

c++ - CMake在自定义命令中编译C++文件