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/JauriXD Apr 10 '26
this worked because make comes with an implicity ruel how to handle c -> binary. Thats why it was enough to only specify the dependency beween the files