I have a bunch of applications that are built with the same type of make rule:
apps = foo bar baz all: $(apps) foo: foo.o $(objects) $(link) bar: bar.o $(objects) $(link) baz: baz.o $(objects) $(link)
If they had an extension (for example .x
) I could make a pattern rule like:
%.x: %.o $(objects) $(link)
and I wouldn't have to write out a new rule for each app.
But they don't have an extension, and I'm pretty sure that:
%: %.o $(objects) $(link)
won't work (because it specifies that to build any file you can use this rule).
Is there anyway to specify one rule that will cover all the $(apps)
build rules?
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.
$@ is the name of the target being generated, and $< the first prerequisite (usually a source file). You can find a list of all these special variables in the GNU Make manual.
Special characters in a makefile In commands, a percent symbol ( % ) is a file specifier. To represent % literally in a command, specify a double percent sign ( %% ) in place of a single one. In other situations, NMAKE interprets a single % literally, but it always interprets a double %% as a single % .
A simple makefile consists of "rules" with the following shape: target ... : dependencies ... command ... ... A target is usually the name of a file that is generated by a program; examples of targets are executable or object files.
This looks like a job for a static pattern rule:
$(apps) : % : %.o $(objects) $(link)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With