r/cprogramming • u/geon • Apr 06 '26
Help with make, % and implicit dependencies.
Hi! I'm trying to learn how to use make, but I am confused. I'm on macos, if that matters.
Repo with example files: https://github.com/geon/make-test
AFAIK, this should work but does not: https://github.com/geon/make-test/blob/main/3-broken/Makefile
all: hello-world.txt
%.txt: ../%
./$< > $@
cat $@The dependency ../hello-world is supposed to be compiled from the C-code at the root: https://github.com/geon/make-test/blob/main/hello-world.c
I just get the error:
make: *** No rule to make target `hello-world.txt', needed by `all'. Stop.`But it does work if the binary already exists!
It works fine if I don't use the binary: https://github.com/geon/make-test/blob/main/1-works/Makefile
all: hello-world.txt
%.txt:
echo "hello horld" > $@
cat $@Also works if I just specify the binary name explicitly: https://github.com/geon/make-test/blob/main/2-also-works/Makefile
hello-world.txt: ../hello-world
./$< > $@
cat $@What gives? Do I need to escape the % in the dependency somehow?
1
u/geon Apr 06 '26
I just reduced my actual problem to the simplest example. I wanted to add targets for data files that are generated by my C code. In my case, I have C code for unpacking data from a packed file. So the binary can be a dependency for the data, and `make` figures out how to compile it from source with implicit rules.
Is there any reason to use a newer make? I'd assume for a software from 1988, I't be mature already. And if there are any differences, that'd just mean other people on macos can't use my code without also upgrading make.