Home > Enterprise >  Make file "%.o: %.c" gives "No rule to make target"
Make file "%.o: %.c" gives "No rule to make target"

Time:10-01

I am learning about makefiles using the following guide: makefiletutorial

So far, everything was going fine until this example of using "%":

objects = foo.o bar.o all.o
all: $(objects)

# These files compile via implicit rules
# Syntax - targets ...: target-pattern: prereq-patterns ...
# In the case of the first target, foo.o, the target-pattern matches foo.o and sets the "stem" to be "foo".
# It then replaces the '%' in prereq-patterns with that stem
$(objects): %.o: %.c

all.c:
    echo "int main() { return 0; }" > all.c

%.c:
    touch $@

clean:
    rm -f *.c *.o all

The example works fine. However, I when I replace the $(objects): %.o: %.c part with %.o: %.c and run make all or make -r all commands I get make: *** No rule to make target 'all.o', needed by 'all'. Stop.

Shouldn't the "%.o" target in the %.o: %.c rule allow the making of any ".o" files? Why do I have to use static pattern matching for the files of the list and not directly use a rule for all ".o" files?

Here is the full non-working makefile:

objects = foo.o bar.o all.o
all: $(objects)

# These files compile via implicit rules
# Syntax - targets ...: target-pattern: prereq-patterns ...
# In the case of the first target, foo.o, the target-pattern matches foo.o and sets the "stem" to be "foo".
# It then replaces the '%' in prereq-patterns with that stem
#$(objects): %.o: %.c
%.o: %.c

all.c:
    echo "int main() { return 0; }" > all.c

%.c:
    touch $@


clean:
    rm -f *.c *.o all

CodePudding user response:

This is not a rule:

%.o: %.c

This is a canceling of a rule. See Canceling Rules. It's not possible in GNU make to create prerequisites with no recipe using pattern rules.

  • Related