What is a makefile?

A makefile is a specification file used by build tools to create an executable version of an application. The makefile details the files, dependencies, and rules to compile and link the target application.

A makefile automates the application building process

The use of a makefile simplifies the development process by automatically performing tasks necessary to rebuild an application when the application source code has been modified.

Components of a makefile

A makefile contains the following elements:

Explicit Rules - These specify how to remake one or more targets and lists prerequisites of the target Implicit Rules - These specify how to make a class of files based on their names. Variable Definitions - These specify a value for a variable that can be used within the makefile. Directives - A directive is a command to do perform a specific function Comments - Comments improve the readability of the makefile are ignored by the make interpreter

Declaring a variable in a makefile

In a makefile, as with most other scripting languages, variables are assigned using the equals sign:

Variable names used by the make interpreter

Variable names can consist of any sequence of characters that does not contain the colon, hash, railway symbols. Variable names cannot contain leading or trailing whitespace.

It is traditional to use a combination of alphanumeric or underscore characters, and to not use a numeral as the first character.

Case Sensitivity

The Make interpreter, which processes the makefile is case sensitive , which means that it interprets names containing lowercase letters as being different from names containing uppercase letters:

# These variables are all different dog='Benjamin'; Dog='Samba'; DOG='Bernie';

Convention

It is conventional to use upper case letters in variable names that control implicit rules or for parameters that the builder can override with command options, and lower case letters for variable names that are used internally within in the makefile.

It is recommended that variable names containing characters other than letters, numbers, and underscores are avoided, as they may be given special meanings in the future, and they may be difficult to pass to other scripts via the shell.

Special Variables

Some variable names that consist of a single punctuation character or just a few characters have specialized uses within the makefile.

Editing a makefile

Useful makefile targets