'Makefile pattern rule not working properly
Long story short, it seems like pattern rules are not behaving correctly.
Assume you have simple Makefile like this,
%.o:
gcc $< -o $@
echo from %.o
and if you type make asd, which clearly doesn't match %.o pattern, it will actually match asd.o and performe recipe for it.
[user]$ make asd
gcc -o asd.o
gcc: fatal error: no input files
compilation terminated.
make: *** [Makefile:2: asd.o] Error 1
So you get this output.
But even stranger thing is that if you explicitly disable the builtin rules by --no-builtin-rules flag, it won't match.
[user]$ make --no-builtin-rules asd
make: *** No rule to make target 'asd'. Stop.
I have no idea why enabling or disabling builtin rule should do anything about pattern matching, so does anyone have idea why this is happening?
P.S. I'm using GNU Make 4.3 in Arch Linux (don't know kernel version).
Solution 1:[1]
which clearly doesn't match %.o pattern
You've specified that to create asd.o you have no dependencies so it will try to create it from your recipe directly.
If you have dependencies, like "to create asd.o there must be an asd.c file", then specify that by adding a dependency:
%.o: %.c
gcc $< -c -o $@
@echo from $@
Note: I added -c to make it into a plain object file, not an executable.
If you now do make --no-builtin-rules asd.o and if you don't have asd.c, it will print:
$ make --no-builtin-rules asd.o
make: *** No rule to make target 'asd.o'. Stop.
If you on the other hand do have asd.c, it'll use your recipe to create asd.o:
$ make --no-builtin-rules asd.o
gcc asd.c -o asd.o
from asd.o
You may want to add a rule for creating an actual executable too - and if you don't want the built-in rules, you can create an empty target for .SUFFIXES:
.SUFFIXES:
%: %.o
gcc -o $@ $<
echo from $@
%.o: %.c
gcc $< -c -o $@
@echo from $@
Now, it'll first create asd.o and then link that into asd if you do make asd:
$ make asd
gcc asd.c -c -o asd.o
from asd.o
gcc -o asd asd.o
echo from asd
from asd
rm asd.o
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
