I've seen that
make is useful for large projects, especially with confusing dependencies described in a
Makefile, and also helping with workflow.
I haven't heard any advantages for using
make for small projects.
Are there any?
As opposed to what?
Suppose you have a program that you have split into two files,
which you have imaginatively named
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?
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.
If you link your app from 2 sources (
.c files) , you do not need to recompile each file, but only the changed one if you are using make.
Also, I will give you example from BSD world. They have framework of system-based Makefiles. They provide you paths to system directories and have targets to install your software and manual pages.
For example, you just wrote
beer.c app and manual for it called
PROG= beer MAN= beer.6 .include <bsd.prog.mk>
make install. It automatically compiles and installs your app to
/usr/bin and compiles and installs your man page to the place where
man can find it. You just installed your app with one simple command!
Very convenient and absolutely transparent for anyone who is familiar with BSD. Much better than manual script.
Makefile for my very small project:
It does exactly what its name says, taking two optional arguments, the coordinates.
I especially like the way things get dependent there.
COORDS ?= 0 0 CXX := g++-8 CXXFLAGS := -std=c++17 -Wall -Wextra -Werror -Wpedantic -pedantic-errors LDLIBS := -lX11 RM := rm -f BIN := getPixelColor SRC := $(BIN).cpp $(BIN): $(SRC) $(CXX) $(CXXFLAGS) $(SRC) -o $(BIN) $(LDLIBS) .PHONY: clean clean: $(RM) $(BIN) .PHONY: run run: $(BIN) ./$(BIN) $(COORDS)
As you can see, it can do all you need, without typing anything extra:
You can run it these ways:
Clean-up the old binary:
Compile a new binary:
Run the executable in 2 ways:
the default coordinates [0,0]
make run # equals COORDS='0 0'
any given coordinates
COORDS='5 6' make run
Makefiles can be extremely helpful at times. The bigger the project, the bigger the benefit. But even with that my smallest C++ project, as you can see on examples saves you a lot of headaches.
make is pretty reliably available. If you distribute your project with a
makefile, users will have a simple reference for how to achieve tasks in the same way you do. The
makefile can be for more than just compilation.
Take a project that doesn't require compilation, for example. I recall working on a Python project that had a make command to clear out all the
.pyc files, a make command to run the tests, one to download a copy of the static data from the development server, etc.