首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cmake add_custom_command不工作

cmake add_custom_command不工作
EN

Stack Overflow用户
提问于 2015-10-13 00:05:17
回答 1查看 6.9K关注 0票数 5

我正在尝试从一个gperf文件中运行cmake

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

当我经过的时候

代码语言:javascript
复制
$ cmake .
$ make 

它不创建example.hpp文件。

下面的CMakeLists.txt有什么问题?

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-13 07:59:11

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

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

代码语言:javascript
复制
# 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()

代码语言:javascript
复制
add_custom_target(my_target
    ALL # Force target to be built with default build target.
    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/example.hpp
)

注意,add_custom_commandadd_custom_target之间的链接在相应的OUTPUTDEPENDS选项中使用相同的文件名表示。有了这样的链接,这些命令的顺序就无关紧要了(但这两个命令都应该从同一个CMakeLists.txt脚本中调用)。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33092108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档