我尝试了这个虚拟的例子:
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 )
#include <stdio.h>
#include "testcmake.h"
int main()
{
int i;
for ( i = 0 ; i < testcmake_txt_len ; i++ )
{
fputc( testcmake_txt[ i ] , stdout );
}
}
foo
bar
baz
失败了:
[...]
xxd: testcmake.txt: No such file or directory
[...]
添加WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
使一切正常但我不希望我的自定义命令的输出出现在我的源目录中,我希望所有中间文件都保留在CMake构建目录中,就像任何非自定义规则一样
答案 0 :(得分:5)
在执行xxd之前,您需要将testcmake.txt复制到构建文件夹。您还需要将构建目录添加到包含,以便#include "testcmake.h"
起作用:
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})
答案 1 :(得分:2)
使用CMake 3.2,file
获得了一项新功能。引自发布公告:
&#34;文件(GENERATE)&#34;命令现在可以生成文件了 用作构建系统目标的源文件。
考虑到您可以切换到CMake 3.2,这可能更容易使用。