1.14. Obtaining GNU make

Problem

You want to obtain and install the GNU make utility, useful for building libraries and executables from source code.

Solution

The solution depends on your operating system.

Windows

While you can obtain prebuilt binaries for GNU make from several locations, to get the most out of GNU make it should be installed as part of a Unix-like environment. I recommend using either Cygwin or MSYS, which is a part of the MinGW project.

Tip

Cygwin and MinGW are described in Recipe 1.1.

If you installed Cygwin, as described in Recipe 1.1, you already have GNU make. To run it from the Cygwin shell, simply run the command make.

To install MSYS, begin by installing MinGW, as described in Recipe Recipe 1.1. A future version of the MinGW installer may give you the option of installing MSYS automatically. For now, follow these additional steps.

First, from the MinGW homepage, http://www.mingw.org, go to the MinGW download area and download the latest stable version of the MSYS installation program. The name of the installation program should be MSYS-<version>.exe.

Next, run the installation program. You will be asked to specify the location of your MinGW installation and the location where MSYS should be installed. When the installation program completes, the MSYS installation directory should contain a file named msys.bat. Running this script will display the MSYS shell, a port of the bash shell from which you can run GNU make and other mingw programs such as g++, ar, ranlib, and dlltool.

Tip

To use MSYS it is not necessary for the bin subdirectories of either your MinGW installation or your MSYS installation to be in your PATH environment variable.

Unix

First, check whether GNU make is installed on your system by running make -v from the command line. If GNU make is installed, it should print a message like the following:

GNU Make 3.80 
Copyright (C) 2002  Free Software Foundation, Inc. 
This is free software; see the source for copying conditions. 
...

If your system has a non-GNU version of make, it’s possible that the GNU version is installed under the name gmake. You can check this by entering gmake -v from the command line.

If you use Mac OS X, the easiest way to obtain GNU make is to download the Xcode development environment from Apple’s web site and follow the installation instructions. Xcode is currently available at http://developer.apple.com/tools.

Otherwise, download the latest version of GNU make from ftp://ftp.gnu.org/pub/gnu/make, unpack it, and follow the installation instructions.

Discussion

The make utility comes in many flavors. Most toolsets provide some variant of make; for example, Visual C++ comes with a make utility called nmake.exe. Usually these toolset-specific versions of make have built-in features which make them easy to use with their particular toolset. As a result, a discussion of make which covers multiple toolsets will either have to describe several versions of make or will have to deal with some cases where there is an imperfect fit between a particular version of make and a particular toolset.

Instead of demonstrating more than one make utility, I’ve chosen to focus on GNU make, which is easily the most powerful and portable make variant. GNU make is designed to work first and foremost with GCC; as a result, using GNU make with other toolsets, particularly Windows toolsets, can be tricky at times. Still, because GNU make is so flexible, it’s far easier to use GNU make with non-GNU tools than it is to use most of the other makes, such as nmake.exe, with a toolset other than the one it was designed for.

Much of GNU make’s power comes from its ability to execute complex shell scripts. If you’ve worked with both Unix and Windows, you know that the Windows shell cmd.exe leaves a lot to be desired: it’s missing many valuable commands, has a limited ability to execute scripts, and places severe restrictions on the length of command lines. Consequently, forcing GNU make to use cmd.exe severely limits its usefulness. Fortunately, Cygwin and MSYS provide excellent environments for using GNU make on Windows.

MSYS provides the minimal environment necessary to run Unix-style makefiles and configure scripts on Windows. Among the useful tools it provides are awk, cat, cp, grep, ls, mkdir, mv, rm, rmdir, and sed. MSYS was designed to work with GCC, and it does so beautifully; it works somewhat less smoothly with other Windows toolsets, however, particularly those that provide .bat files for setting environment variables and those that use slashes (/) instead of a hyphens (-) for command-line options.

Where MSYS is minimalist, Cygwin is maximalist. Cygwin make can do everything MSYS make can do, and much more. Portable makefiles, however, restrict themselves to a narrow range of GNU utilities, and MSYS supports all of these.

See Also

Recipe 1.1

Get C++ Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.