Karl Yngve Lervåg
Karl Yngve Lervåg

Reputation: 1715

Use general rule as part of a specific target in Gnu Make

I am curious if it is possible to use a general rule as part of a more specific rule in Gnu Make. This is easier to explain with an example:

%.o:
        $(FC) $(FLAGS) -o $@ -c $<


some_file.o:
        DO SOMETHING EXTRA
        PASS ON TO GENERAL FOR %.o

That is, I want the target for some_file.o to first do something extra, and then do what is specified for %.o. Of course, I could just be redundant and write

some_file.o:
        DO SOMETHING EXTRA
        $(FC) $(FLAGS) -o $@ -c $<

But that is not as convenient.

Upvotes: 1

Views: 147

Answers (2)

Brave Sir Robin
Brave Sir Robin

Reputation: 1046

Add an extra rule that does not create the file itself:

%.o:
    $(FC) $(FLAGS) -o $@ -c $<

some_file.o: thing

thing:
    DO SOMETHING EXTRA BUT DON'T CREATE some_file.o

Note that if thing is not created, this will cause some_file.o to be built every time.

Upvotes: 1

MadScientist
MadScientist

Reputation: 100781

No, that's not possible in any reasonable way. The best you can do is put the command into a variable, then reuse the variable:

FCCOMMAND = $(FC) $(FLAGS) -o $@ -c $<

%.o :
        $(FCCOMMAND)

some_file.o:
        DO SOMETHING EXTRA
        $(FCCOMMAND)

Upvotes: 0

Related Questions