The GNU make Utility and Makefiles
Explore how to use the GNU make utility and writing Makefiles to simplify compiling and linking C programs composed of multiple files. Understand how Makefiles track dependencies, automate builds, and speed up recompilation by only rebuilding changed parts. Gain skills to organize compilation steps efficiently and reduce errors in building executables.
We'll cover the following...
The make utility
There is a UNIX tool called make that’s commonly used to compile C programs that are made up of several files and (sometimes) involve several compilation steps. There’s a lot of power in the make tool, but what we want to introduce here is a simple use of it, which lets us avoid having to remember a long, complicated compile command (e.g., on line 1 of the output from the prime number program we saw in the previous lesson).
The make utility uses a special plain text file that we write and that has to reside in the same directory as our program and has to be called Makefile. We can think of a Makefile as a recipe for making our program (i.e. linking and compiling).
The Makefile file
A simple Makefile for our prime number program from the previous lesson might look like this:
-
The first word and colon
go:on line 1 represents the name of the executablegowhich is to be built. This executable is called the target.The list of files after the colon (
go.c primes.c) represents all of the things that the targetgodepends upon. These are called ...