Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile pattern rule referencing stem in dependencies

I want a pattern rule with dependencies constructed both from the stem and using wildcards, i.e. something like

$(FILES): %.o: %.c $(wildcard %*.c)

This doesn't seem to work: the stem % is not expanded within the wildcard function (see http://www.gnu.org/software/make/manual/html_node/Pattern-Rules.html), while the automatic variable $* seems not to be recognized when listing dependencies.

Is there a (not too kludgy) way of doing something like this?

like image 428
Jaap Eldering Avatar asked Jul 09 '11 19:07

Jaap Eldering


People also ask

What is pattern rule in makefile?

A pattern rule looks like an ordinary rule, except that its target contains the character ' % ' (exactly one of them). The target is considered a pattern for matching file names; the ' % ' can match any nonempty substring, while other characters match only themselves.

What does $() mean in makefile?

The $@ and $< are called automatic variables. The variable $@ represents the name of the target and $< represents the first prerequisite required to create the output file.

Can you have multiple rules in a makefile?

There can only be one recipe to be executed for a file. If more than one rule gives a recipe for the same file, make uses the last one given and prints an error message. (As a special case, if the file's name begins with a dot, no error message is printed.

How do you use wildcards in makefile?

If you want to do wildcard expansion in such places, you need to use the wildcard function, like this: $(wildcard pattern ...) This string, used anywhere in a makefile, is replaced by a space-separated list of names of existing files that match one of the given file name patterns.


1 Answers

As is stated in section 10.5.3 of the GNU make manual, automatic variables (which is what $* is) are not available in prerequisites, but it also refers to a work-around, namely secondary expansion.

If I understand it (and you) correctly, something like this should do what you want:

.SECONDEXPANSION:
$(FILES): %.o: %.c $$(wildcard $$**.c)
like image 65
eriktous Avatar answered Sep 21 '22 15:09

eriktous