1.15. Building A Simple “Hello, World” Application with GNU make
Problem
You want to use GNU make to build a simple “Hello, World” program, such as that in Example 1-4.
Solution
Before you write your first makefile, you’ll need to know a little terminology. A makefile consists of a collection of rules of the form
targets:prerequisitescommand-script
Here targets and prerequisites are space-separated strings, and command-script
consists of zero or more lines of text, each of which begins with a Tab character. Targets
and prerequisites are usually files names, but sometimes they are simply formal names for
actions for make to perform. The command script
consists of a sequence of commands to be passed to a shell. Roughly speaking, a rule tells
make to generate the collection of targets from the
collection of prerequisites by executing the command script.
Tip
Whitespace in makefiles is significant. Lines containing command scripts must begin with a Tab rather than a Space — this is a source of some of the most common beginner errors. In the following examples, lines which begin with a Tab are indicated by an indentation of four characters.
Now you’re ready to begin. Create a text file named makefile in the directory containing your source file. In this file, declare
four targets. Call the first target all, and specify the name of the executable you wish to build as its sole prerequisite. It should have no command script. Give the second target the same name as your executable. Specify your ...