Chapter 3. GNU make
The GNU make utility, commonly abbreviated make, is a tool for controlling the generation of executables from source files. make automatically determines which parts of a complex program have changed and need to be recompiled. make uses configuration files called Makefiles to control the way programs are built.
Red Hat Developer Toolset is distributed with make 4.3. This version is more recent than the version included in Red Hat Enterprise Linux and provides a number of bug fixes and enhancements.
3.1. Installing make
In Red Hat Developer Toolset, GNU make is provided by the devtoolset-11-make package and is automatically installed with devtoolset-11-toolchain as described in Section 1.5, “Installing Red Hat Developer Toolset”.
3.2. Using make
To build a program without using a Makefile, run the make
tool as follows:
$ scl enable devtoolset-11 'make source_file_without_extension'
This command makes use of implicit rules that are defined for a number of programming languages, including C, C++, and Fortran. The result is a binary file named source_file_without_extension in the current working directory.
Note that you can execute any command using the scl
utility, causing it to be run with the Red Hat Developer Toolset binaries used in preference to the Red Hat Enterprise Linux system equivalent. This allows you to run a shell session with Red Hat Developer Toolset make
as default:
$ scl enable devtoolset-11 'bash'
To verify the version of make
you are using at any point:
$ which make
Red Hat Developer Toolset’s make
executable path will begin with /opt
. Alternatively, you can use the following command to confirm that the version number matches that for Red Hat Developer Toolset make
:
$ make -v
Example 3.1. Building a C Program Using make
Consider a source file named hello.c
with the following contents:
#include <stdio.h> int main(int argc, char *argv[]) { printf("Hello, World!\n"); return 0; }
Build this source code using the implicit rules defined by the make
utility from Red Hat Developer Toolset:
$ scl enable devtoolset-11 'make hello'
cc hello.c -o hello
This creates a new binary file called hello
in the current working directory.
3.3. Using Makefiles
To build complex programs that consist of a number of source files, make uses configuration files called Makefiles that control how to compile the components of a program and build the final executable. Makefiles can also contain instructions for cleaning the working directory, installing and uninstalling program files, and other operations.
make automatically uses files named GNUmakefile
, makefile
, or Makefile
in the current directory. To specify another file name, use the -f
option:
$ make -f make_file
Describing the details of Makefile syntax is beyond the scope of this guide. See GNU make, the upstream GNU make manual, which provides an in-depth description of the GNU make utility, Makefile syntax, and their usage.
The full make manual is also available in the Texinfo format as a part of your installation. To view this manual:
$ scl enable devtoolset-11 'info make'
Example 3.2. Building a C Program Using a Makefile
Consider the following universal Makefile named Makefile
for building the simple C program introduced in Example 3.1, “Building a C Program Using make”. The Makefile defines some variables and specifies four rules, which consist of targets and their recipes. Note that the lines with recipes must start with the TAB character:
CC=gcc CFLAGS=-c -Wall SOURCE=hello.c OBJ=$(SOURCE:.c=.o) EXE=hello all: $(SOURCE) $(EXE) $(EXE): $(OBJ) $(CC) $(OBJ) -o $@ .o: .c $(CC) $(CFLAGS) $< -o $@ clean: rm -rf $(OBJ) $(EXE)
To build the hello.c
program using this Makefile, run the make
utility:
$ scl enable devtoolset-11 'make'
gcc -c -Wall hello.c -o hello.o
gcc hello.o -o hello
This creates a new object file hello.o
and a new binary file called hello
in the current working directory.
To clean the working directory, run:
$ scl enable devtoolset-11 'make clean'
rm -rf hello.o hello
This removes the object and binary files from the working directory.
3.4. Additional Resources
For more information about the GNU make tool and its features, see the resources listed below.
Installed Documentation
make(1) — The manual page for the
make
utility provides information on its usage. To display the manual page for the version included in Red Hat Developer Toolset:$
scl enable devtoolset-11 'man make'
The full make manual, which includes detailed information about Makefile syntax, is also available in the Texinfo format. To display the info manual for the version included in Red Hat Developer Toolset:
$
scl enable devtoolset-11 'info make'
Online Documentation
- GNU make — The upstream GNU make manual provides an in-depth description of the GNU make utility, Makefile syntax, and their usage.
See Also
- Chapter 1, Red Hat Developer Toolset — An overview of Red Hat Developer Toolset and more information on how to install it on your system.
- Chapter 2, GNU Compiler Collection (GCC) — Instructions on using the GNU Compiler Collection, a portable compiler suite with support for a wide selection of programming languages.
- Chapter 4, binutils — Instructions on using binutils, a collection of binary tools to inspect and manipulate object files and binaries.
- Chapter 5, elfutils — Instructions on using elfutils, a collection of binary tools to inspect and manipulate ELF files.
- Chapter 6, dwz — Instructions on using the dwz tool to optimize DWARF debugging information contained in ELF shared libraries and ELF executables for size.
- Chapter 8, GNU Debugger (GDB) — Instructions on debugging programs written in C, C++, and Fortran.