添加一个以文件名作为目标的自定义命令 [英] adding a custom command with the file name as a target
问题描述
我想做一些类似 add_custom_command
,输出文件
name作为生成的makefile中的目标。有没有一个优雅的方式
这样做?
I'd like to do something like add_custom_command
, with the output file
name as a target in the generated makefile. Is there an elegant way of
doing this?
我见过的所有例子(如 cmake faq re:latex )使用 add_custom_command
来说明如何生成所需的输出文件,然后 add_custom_target
创建目标。例如:
All the examples I've seen (such as the cmake faq re: latex) use add_custom_command
to tell how to generate the desired output file, and then add_custom_target
to create a target. e.g.:
add_executable (hello hello.c)
add_custom_command(OUTPUT hello.bin
COMMAND objcopy --output-format=binary hello hello.bin
DEPENDS hello
COMMENT "objcopying hello to hello.bin")
add_custom_target(bin ALL DEPENDS hello.bin)
但是,生成的makefile中的目标名称是 bin
$ b比 hello.bin
。是否有一种方法使 hello.bin
本身成为生成的makefile中的目标
?
However, the target name in the generated makefile is then bin
rather
than hello.bin
. Is there a way to make hello.bin
itself a target
in the generated makefile?
解决方案我尝试不工作:
Some solutions I've tried that don't work:
- 更改为:
add_custom_target(hello.bin ALL DEPENDS hello .bin)
会导致makefile中的循环依赖。
- changing to:
add_custom_target(hello.bin ALL DEPENDS hello.bin)
results in a circular dependency in the makefile.
推荐答案
p>你可以通过生成你的hello.bin作为目标的副作用。而不是从objcopy生成hello.bin,您生成hello.tmp。然后作为副作用,你也复制hello.tmp到hello.bin。最后,你创建依赖于你的hello.tmp的假目标hello.bin。在代码中:
You could do it by generating your hello.bin as a side effect of a target. Instead of generating hello.bin from objcopy, you generate hello.tmp. Then as a side effect you also copy hello.tmp to hello.bin. Finally, you create the phony target hello.bin that depends on your hello.tmp. In code:
add_executable (hello hello.c)
add_custom_command(OUTPUT hello.tmp
COMMAND objcopy --output-format=binary hello hello.tmp
COMMAND ${CMAKE_COMMAND} -E copy hello.tmp hello.bin
DEPENDS hello
COMMENT "objcopying hello to hello.bin")
add_custom_target(hello.bin ALL DEPENDS hello.tmp)
问题是hello .bin在运行clean时不会被清除。要使其工作,请添加:
The problem with that is that hello.bin is not cleaned when you run clean. To get that working, add:
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES hello.bin)
这篇关于添加一个以文件名作为目标的自定义命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!