Search⌘ K
AI Features

The GNU make Utility and Makefiles

Explore the GNU make utility and Makefiles to simplify compiling C programs that involve multiple source files. Learn how to write rules, set dependencies, use macros for flexible compilation, and speed up rebuilds by recompiling only changed files. This lesson helps you organize and automate compilation workflows effectively.

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:

C++
go: go.c primes.c
gcc -o go go.c primes.c
  • The first word and colon go: on line 1 represents the name of the executable go which is to ...