Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
Makefile overriding default implicit rule

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)
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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 from n.o by running the linker (usually called ld) 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 and z.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.

over 4 years ago · Santiago Trujillo Report

0

Your rule isn't the same as the built-in implicit rule so it doesn't cancel it.

Additionally, make always prefers a rule that doesn't require intermediate files to be built to one that does. If you pre-create the .a file make might use your rule instead (but it might not even still).

If you cancel the built-in rule and leave your rule in place it should work correctly I believe.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!