问题描述
我必须在现有的Makefile
中集成许多HTML文件的生成.问题在于HTML文件需要驻留在许多不同的目录中.我的想法是编写一个隐式规则,将源文件(* .st)转换为相应的html文件
I have to integrate the generation of many HTML files in an existing Makefile
.The problem is that the HTML files need to reside in many different directories.My idea is to write an implicit rule that converts the source file (*.st) to the corresponding html file
%.html: %.st
$(HPC) -o $@ $<
以及依赖于所有html文件的规则
and a rule that depends on all html files
all: $(html)
如果HTML文件不在builddir中,则 make 找不到隐式规则:*** No rule to make target
.如果我这样更改隐式规则
If the HTML file is not in the builddir, make doesn't find the implicit rule: *** No rule to make target
.If I change the implicit rule like so
$(rootdir)/build/doc/2009/06/01/%.html: %.st
$(HPC) -o $@ $<
已找到,但随后我必须对项目中几乎每个文件都有一个隐式规则.根据隐式规则搜索算法 GNU make
手册,规则搜索的工作方式如下:
it's found, but then I have to have an implicit rule for nearly every file in the project.According to Implicit Rule Search Algorithm in the GNU make
manual, rule search works like this:
- 将整个目标名称 t 分为目录部分,称为 d ,其余部分称为 n .为了 例如,如果 t 是
src/foo.o
, 那么 d 是src/
, 而 n 是foo.o
. - 列出所有模式规则的列表,其目标之一与 t 或 n 相匹配. 如果目标模式包含斜线, 它与 t 匹配; 否则,针对 n .
- Split the entire target name t into a directory part, called d, and the rest, called n. For example, if t is
src/foo.o
, then d issrc/
, and n isfoo.o
. - Make a list of all the pattern rules one of whose targets matches t or n. If the target pattern contains a slash, it is matched against t; otherwise, against n.
为什么找不到隐式规则,并且假设使用了GNU make
,哪一种是最优雅的解决方案?
Why is the implicit rule not found, and what would be the most elegant solution, assuming GNU make
is used?
这是我的Makefile
的精简版本:
rootdir = /home/user/project/doc
HPC = /usr/local/bin/hpc
html = $(rootdir)/build/doc/2009/06/01/some.html
%.html: %.st
$(HPC) -o $@ $<
#This works, but requires a rule for every output dir
#$(rootdir)/build/doc/2009/06/01/%.html: %.st
# $(HPC) -o $@ $<
.PHONY: all
all: $(html)
推荐答案
像Maria Shalnova一样,我喜欢递归生成(尽管我不同意递归生成被认为有害"),通常最好从源代码那里获取内容,而不是相反.但是,如果必须的话,我建议稍作改进:让generateHtml仅生成规则,而不生成命令.
Like Maria Shalnova I like recursive make (though I disagree with "Recursive Make Considered Harmful"), and in general it's better to make something HERE from a source THERE, not the reverse. But if you must, I suggest a slight improvement: have generateHtml generate only the RULE, not the COMMANDS.
这篇关于GNU make有许多目标目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!