Makefile生成器创建两个文件 [英] Makefile generator creates two files
问题描述
我有一个生成器程序,可以创建两个版本文件,例如ver.h
和ver.cpp
.我最终的构建目标取决于这两个文件,而构建这两个文件的规则是一个程序.如果我这样做:
I have a generator program that creates two version files, say ver.h
and ver.cpp
. My ultimate build target depends on both of these files, and the rule for building both is that one program. If I did this:
build : ver.h ver.cpp
ver.h ver.cpp :
./gen/version/program
然后并行构建可以运行program
两次,这虽然不是 bad ,但这只是多余的.我想我可以让它们都依赖于伪造的目标:
then a parallel build could run program
twice, which, while not bad is just excessive. I figure I could have them both depend on a phony target:
ver.h ver.cpp : do-version-impl
do-version-impl:
./gen/version/program
.PHONY : do-version-impl
这是最好的方法吗?必须引入一个假音规则来做到这一点,这听起来有点可笑.
Is that the best way to do this? It smells a little funny to have to introduce a phony rule to do this.
推荐答案
将假目标作为前提是一个坏主意.即使存在ver.*
文件,也会运行program
,这是一个误报错误.
Using the phony target as the prerequisite is a bad idea. program
will be run even if ver.*
files exist, which is a false positive error.
更妙的是,仅当GNU Make文件是配方规则的目标时,才保证更新其文件时间戳.因此,即使program
始终运行,反过来依赖于ver.*
文件的所有内容也可能根本无法更新!
More subtly, GNU Make is only guaranteed to update its file timestamp, if that file is a target of a rule with a recipe. So here, even though program
is always run, anything that in turn depends on ver.*
files might not get updated at all!
我认为最好不要为每个目标组成不自然的模式,而要明确:
In my opinion it is best to not make up unnatural patterns for each target, but instead, go explicit:
您正在生成一个主"文件,即ver.cpp
.将"no-op"食谱;
用于另一个食谱,可以像这样将其放在同一行上:
There is a "main" file that you are generating, that is ver.cpp
. Use the "no-op" recipe ;
for the other one, which can be put on the same line like this:
ver.h: ver.cpp ;
ver.cpp: Makefile
./gen/version/program
此方法从您编写的内容开始,但添加了非常重要的;
.
This method starts with what you wrote, but adds the very important ;
.
如果您没有主"文件的自然候选人,那么我认为最好使用前哨":
If you did not have a natural candidate for the "main" file, then in my opinion it is best to use a "sentinel":
ver.h ver.cpp: sentinel ;
sentinel: Makefile
./gen/version/program
touch $@
同样,此方法与您的方法之一相似,但非常重要的是,它不使用假音,而是使用真实文件.
Again, this method is similar to one of your methods, but very importantly, does not use a phony, but a real file.
这篇关于Makefile生成器创建两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!