使用CMake add_custom_command为另一个目标生成源 [英] Use CMake add_custom_command to generate source for another target

查看:2034
本文介绍了使用CMake add_custom_command为另一个目标生成源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过这个虚拟示例:

cmake_minimum_required( VERSION 2.8 )
project( testcmake )

add_custom_command(
  OUTPUT testcmake.h
  COMMAND xxd -i testcmake.txt testcmake.h
  DEPENDS testcmake.txt
)

add_executable( testcmake testcmake.c testcmake.h )



testcmake.c



testcmake.c

#include <stdio.h>
#include "testcmake.h"

int main()
{
    int i;

    for ( i = 0 ; i < testcmake_txt_len ; i++ )
    {
        fputc( testcmake_txt[ i ] , stdout );
    }
}



testcmake.txt



testcmake.txt

foo
bar
baz


b $ b

问题



它失败:

The problem

It fails with:

[...]
xxd: testcmake.txt: No such file or directory
[...]

添加 WORKING_DIRECTORY $ {CMAKE_CURRENT_SOURCE_DIR} 使一切正常,但我不想我的自定义命令的输出出现在我的源目录,我想要所有的中间文件保留在CMake的构建目录,就像任何非自定义的规则。

Adding WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} makes everything works fine but I don't want the output of my custom command appears in my source directory, I want that all the intermediate files remain in the CMake build directory just like any non custom rule.

推荐答案

.txt到执行xxd之前的构建文件夹。您还需要将您的构建目录添加到包含,以便 #includetestcmake.h工程:

You need to copy testcmake.txt to your build folder before executing xxd. You'll also need to add your build directory to the includes so that #include "testcmake.h" works:

add_custom_command(
  OUTPUT testcmake.h
  COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/testcmake.txt testcmake.txt
  COMMAND xxd -i testcmake.txt testcmake.h
  DEPENDS testcmake.txt
)

include_directories(${CMAKE_CURRENT_BINARY_DIR})

这篇关于使用CMake add_custom_command为另一个目标生成源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆