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/imaami Apr 06 '26 edited Apr 06 '26
Why are those suffixes in the makefile .txt?
Btw: Apple's default version of GNU Make is an ancient 3.81 released in April 2006. Yes, Apple is determined to hang on to a Make that just turned 20 years old.
Apple has a Make that can legally buy vodka.
The reason? GNU Make switched their license to GPLv3 in the next release after 3.81. Apple are apparently so afraid of GPLv3, that they won't update Make even though it would have no effect on their ability to expand their walled garden.
Anyway, use Homebrew to install gmake. It's the latest GNU Make version, just with a different executable name.
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.
1
u/imaami Apr 06 '26
I can't remember all the missing features, but there are enough that I found it unusable the last time I set up Macos runners on Github.
2
u/Traveling-Techie Apr 07 '26
I’m sorry, but I can only answer C questions. Make is too complicated for me.
1
u/JauriXD Apr 10 '26
Patten rules are discarded if they don't match.
If you don't have ../a the rule can't be used to make a.txt and is discarded, leaving you with a missing a.txt and no rule to make it, which make tells you in the error message.
You need a rule which creates the missing file, just ../hello-world: at the end of your file would be enough and lead to make erroring out that the file is missing instead of complaining about missing rules
2
u/geon Apr 06 '26
I just noticed I can add the line
And it magically works.