I l@ve RuBoard Previous Section Next Section

7.5 The Makefile

Once the source has been entered, it needs to be compiled and linked. Up to now we have been running the compiler manually. This is somewhat tedious and prone to error. Also, larger programs consist of many modules and are extremely difficult to compile by hand. Fortunately, both Unix and Borland-C++ have a utility called make that handles the details of compilation. Microsoft Visual C++ comes with the same thing, but the program is named nmake.

Basically, make looks at the file called Makefile for a description of how to compile your program and then runs the compiler for you. The makeprogram is discussed in detail in Chapter 23. For now, just use one of the examples below as a template and substitute the name of your program in place of calc.

The following examples contain Makefiles for various C++ compilation environments. These include:

  • The Unix operating system using a generic CC compiler (Example 7-2)

  • The Unix or Linux operating system using the Free Software's g++ compiler (Example 7-3)

  • The Microsoft Windows operating system using the Borland C++ compiler (Example 7-4)

  • The Microsoft Windows operating system using the Microsoft Visual Studio .NET C++ compiler (Example 7-5)

Example 7-2. Makefile for CC under Unix
#
# Makefile for many Unix compilers using the
# "standard" command name CC
#
CC=CC
CFLAGS=-g
all: calc

calc: calc.cpp
$(CC) $(CFLAGS) -o calc calc.cpp

clean:
rm calc
Example 7-3. Makefile for GNU g++ under Linux or Unix
#
# Makefile for the Free Software Foundation's g++ compiler
#
CC=g++
CFLAGS=-g -Wall
all: calc

calc: calc.cpp
        $(CC) $(CFLAGS) -o calc calc.cpp

clean:
        rm calc
Example 7-4. Makefile for Borland C++ on Windows
#
# Makefile for Borland's Borland-C++ compiler
#
CC=bcc32
#
# Flags 
#       -N  -- Check for stack overflow
#       -v  -- Enable debugging
#       -w  -- Turn on all warnings
#       -tWC -- Console application
#
CFLAGS=-N -v -w -tWC
all: calc.exe

calc.exe: calc.cpp
        $(CC) $(CFLAGS) -ecalc calc.cpp

clean:
        erase calc.exe
Example 7-5. Makefile for Microsoft Visual Studio .NET C++ on Windows
#
# Makefile for Microsoft Visual C++
#
CC=cl
#
# Flags 
#       GZ -- Enable stack checking
#       RTCsuc -- Enable all runtime checks
#       Zi -- Enable debugging
#       Wall -- Turn on warnings (Omitted)
#       EHsc -- Turn exceptions on

#
CFLAGS=/GZ /RTCsuc /Zi /EHsc 
all: calc.exe

calc.exe: calc.cpp
        $(CC) $(CFLAGS)  calc.cpp

clean:
        erase calc.exe

To compile the program, just execute the command make. (Under Microsoft Visual C++ use the command nmake.)Themake program determines what compilation commands are needed and executes them.

For example, on Linux and Unix we compile our program with the command:

$ make 
g++ -g -Wall -o calc calc.c
calc.cpp: In function 'int main(  )':
calc.cpp:19: warning: suggest parenthesis around assignment used as truth value
$ 

Using Microsoft Visual C++ the command is:

C:> nmake
Microsoft (R) Program Maintenance Utilitity Version 7.00.9392
Copyright (C) Microsoft Corporation. All rights reserved.

cl /FeCALC /RTCsuc /Zi /Wall calc.cpp
Microsoft (R) 32-bit C/C++ Compiler Ver. 13.00.9392 for 80x86
Copyright (C) Microsoft Corporation 1984-2001. All rights reserved.
/out:CALC.exe
/debug
calc.obj
LINK : LNK6004: CALC.exe not found or not built by the last incremental link; 
performing full link

The make program uses the modification dates of the files to determine whether a compilation is necessary. Compilation creates an object file. The modification date of the object file is later than the modification date of its source. If the source is edited, its modification date is updated, making the object file out of date. make checks these dates, and if the source was modified after the object, make recompiles the object.

    I l@ve RuBoard Previous Section Next Section