Why this rule cannot override the default implicit rule ?
When make is invoked like: make myapp
(suppose myapp.c is there). The make runs the default command to build and link the program instead the commands defined in this implicit rule:
#... omitted code
LCUS=$(LIBS)/libcus.a
#... omitted code
% : %.o $(LCUS)
echo !!! Custom build !!!
$(MY_CMD) $< -o $@ $(LCUS)
Taken from the GNU online make
manual:
You can override a built-in implicit rule (or one you have defined yourself) by defining a new pattern rule with the same target and prerequisites, but different commands.
So I would assume it is because the prerequisites are not the same as the implicit rule.
Also take from the make
manual:
Linking a single object file
n
is made automatically fromn.o
by running the linker (usually calledld
) via the C compiler. The precise command used is$(CC) $(LDFLAGS) n.o $(LOADLIBES) $(LDLIBS)
. This rule does the right thing for a simple program with only one source file. It will also do the right thing if there are multiple object files (presumably coming from various other source files), one of which has a name matching that of the executable file. Thus,x: y.o z.o
when
x.c
,y.c
andz.c
all exist will execute:cc -c x.c -o x.o cc -c y.c -o y.o cc -c z.c -o z.o cc x.o y.o z.o -o x rm -f x.o rm -f y.o rm -f z.o
So basically make
understands implicit rules for program files that are generated from .o
files but when you throw in your static library it doesn't understand. A simple way to test would be to remove $(LCUS)
from your dependencies (as a temporary measure) to see if it then uses your rule instead of the built in one. If it does then you know that is your problem. If simply adding myapp
in replace of %
is a problem because you want the rule to build multiple targets you can try the following:
$(APPS): % : %.o $(LCUS)
where $(APPS)
is a variable containing all the applications you wish to build. This will allow one rule to build multiple targets. You could also skip the use of the variable altogether and place a space separated list. This is an example of a static pattern rule, more information can be found here. The difference between static pattern and implicit rules can be found here.
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