What are the advantages of using `make` for small projects?
A lot of other people are getting into the details of more complex makefiles and a lot of the complexity that comes with them. I typically use makefiles for a completely different reason:
I don't want to remember anything.
Even if your project is really boring and simple, and you don't use makefiles "correctly":
all:
gcc main.c -o project
I don't need to think about it or treat it any differently than a project that's more complex:
all:
gcc libA.c libB.c main.c -o project2
Or if I specified flags (e.g. -O2
) I don't need to remember what they were.
Also, if you start with a simple makefile, and you need to merge/refactor things later, you don't need to remember to build every project differently.
As opposed to what?
Suppose you have a program that you have split into two files,
which you have imaginatively named file1.c
and file2.c
.
You can compile the program by running
cc file1.c file2.c -o yourprogram
But this requires recompiling both files every time, even if only one has changed. You can decompose the compilation steps into
cc -c file1.c
cc -c file2.c
cc file1.o file2.o -o yourprogram
and then, when you edit one of the files, recompile only that file (and perform the linking step no matter what you changed). But what if you edit one file, and then the other, and you forget that you edited both files, and accidentally recompile only one?
Also, even for just two files, you’ve got about 60 characters’ worth of commands there. That quickly gets tedious to type. OK, sure, you could put them into a script, but then you’re back to recompiling every time. Or you could write a really fancy, complicated script that checks what file(s) had been modified and does only the necessary compilations. Do you see where I’m going with this?
Even with small project it can be helpful keeping the dependency logic under control and builds automated. I also used it to trigger installs and deinstallations, so it was a main switch resetting the stage.