3 ms·
There is much misunderstanding about Makefiles, what they are, and what they are not. Make is not a "programming language build system" as some would imply, but
by hgs3 2y ago
There is much misunderstanding about Makefiles, what they are, and what they are not. Make is not a "programming language build system" as some would imply, but rather a recipe builder. With Make you provide the file you want built, the shell commands to build it, and any files the build depends on.
Here's a simple example to get you started. Create a file named "Makefile" with the following text and an empty file alongside it named foo.txt.
bar.txt: foo.txt
cp foo.txt bar.txt
When you run the "make" command in your shell it will check if the "bar.txt" file exists. If it does not exist OR if "foo.txt" has a newer timestamp, then it will rebuild "bar.txt" by executing the tab indented shell commands underneath. In this case, the only shell command used is the 'cp' command. In the linked article the author invokes npm, bundler, and netlify.
When people use Make to compile their C code they are simply invoking the C compiler just like they would any other shell command. You might have seen something in a Makefile that looks like this:
foobar.o: foobar.c
gcc -c foobar.c
This is just saying: "The output file 'foobar.o' depends on the input file 'foobar.c' and to build 'foobar.o' run the shell command 'gcc -c foobar.c'" which is conceptually the same as my previous example were we built "bar.txt". Since explicitly listing every .o and .c file in a Makefile is tedious, many authors opt for wildcards.
I hope this helped!
- Jach 2y ago> When you run the "make" command in your shell it will check if the "bar.txt" file exists. I got to this point and ran into an error: Makefile:2: *** missing separator. Stop. Ok I'm just giving you a hard time, and you mention right after the existence of "tab indented" so whatever. Still it's one of the things I detest on an aesthetic level about make, even if my editor has special syntax support for makefiles to handle this archaic requirement of actual tabs without me ever having to worry about it in practice.
- Izkata 2y ago> Still it's one of the things I detest on an aesthetic level about make There's a little-known variable called .RECIPEPREFIX that lets you switch from tabs to anything else. Probably a bad idea to use it in anything shared with anyone else.
- spc476 2y agoThat's for GNU make, not POSIX make [1]. For some people, they either won't, or can't, use GNU make. [1] https://pubs.opengroup.org/onlinepubs/9799919799/utilities/make.html https://pubs.opengroup.org/onlinepubs/9799919799/utilities/m...
- evilotto 2y agoI have a suspicion that a lot of people who rant about makefiles using tabs also praise python for the brilliance of using whitespace for scoping. Or who love yaml for its indented block structure. Nah, who am I kidding ... no one loves yaml, it's just better than most of the alternatives.
- Jach 2y agoI quite like python's whitespace requirements, but notably it doesn't care what your stance is on tabs vs spaces, or how many spaces, so long as you're consistent in a block. Never liked yaml, though, I don't think it's better than any of the alternatives.
- imtringued 2y agoFor what I use YAML for, there are no alternatives for it except obscure formats like HCL.