GNU Makefile - a template rule with multiple targets with one dependency ignores all targets, but the first

I want to create a target audience. In particular: I have one source file, and I want to create different objects that are added to the corresponding language folder. This single source file will differ in C-Flags, the compiler will receive. While I used it in a static way, it works very well.

de/info.o en/info.o es/info.o : info.c $(ECHO) (DEP) $< for $@ 

Now I thought it would be great if it were a bit more dynamic, if I add a new language-dependent file. So I used a wildcard:

 de/%.o en/%.o es/%.o : %.c $(ECHO) (DEP) $< for $@ 

But now he just makes the first goal and ignores the rest. Make-Debug prints the following:

 Successfully remade target file `de/info.o'. Considering target file `en/info.o'. File `en/info.o' was considered already. 

Just in case: No, objects do not exist. Thus, there is no goal, but there is a dependency, so make must follow the rules.

EDIT: Found a solution to this problem.

 define FOO $(1)/%.o : %.c $(ECHO) $$< for $(1) endef $(foreach lang,$(LANGUAGE_LIST), $(eval $(call FOO,$(lang)))) 

Inspired by: http://www.gnu.org/software/make/manual/make.html#Eval-Function

+7
makefile gnu-make
source share
1 answer

Template rules work differently than implicit rules. Although an implicit rule such as

 abc: d command 

equivalent to a longer notation

 a: d command b: d command c: d command 

this is not true for template rules. A template rule with multiple goals is explicitly required to create all of its goals in a single command call. So you have to write

 $ cat GNUmakefile all: de/xo en/xo es/xo de/%.o: %.c @echo $@ from $< en/%.o: %.c @echo $@ from $< es/%.o: %.c @echo $@ from $< $ gmake de/xo from xc en/xo from xc es/xo from xc 

The relevant documentation is contained in 10.5.1. Introduction to the GNU Guidelines Pattern Rules:

Template rules can have more than one purpose. Unlike ordinary rules, this does not apply to so many different rules with the same premises and recipes. If a template rule has several goals, make knows that the rules recipe is responsible for creating all the goals. The recipe is only done once to make all the goals. When searching for a template rule to match the target, the target templates of the rule, other than the one that corresponds to the target that needs the rule, are random: only worry about providing the recipe and the premises for the file in question. However, when this recipe file is launched, other targets are marked as updated themselves.

+6
source share

All Articles