Programming Makefile Template

The following is a template, something upon which you can build, for programming. I will show you the template and indicate what is and is not permissible in the color red


First create a directory with the name Programming. Then 'cd' to this directory and create the following file with the name Makefile:



#####
#       Makefiles can have comments with lines
#       starting with the # sign
#####

#####
#       Define the compilers. There are defaults built
#       into the program Makefile. We will not use them
#       We will use only ONE shorthand to keep this
#       file understandable for humans
#####

FCMP=g77 -O                              [do a 'man gcc' or 'man g77' to understand options]                           
CCMP=gcc -O

.f.o:
        $(FCMP) -c $<                    [This is the rule form converting a source file to
                                          a binary object file for FORTRAN]

.c.o:
        $(CCMP) -c $<                    [Note that all indents MUST be TABS and not spaces]

all: prog1 prog2 \                       [This permits use to compile all at once. Note that the
        prog3                             line continuation is a \[RETURN] not \ [space][return]

prog1: prog1.o                           [Compile a FORTRAN program since the source file is prog1.f] 
        $(FCMP) prog1.o -o prog1

prog2: prog2.o sub2.o                    [Compile a C program with two parts: prog2.c subs2.c]
        $(CCMP) prog2.o sub2.o -o prog2

prog3: prog3.o sub3.o                    [Compile a program consisting of prog3.f and sub3.c]
        $(FCMP) prog3.o sub3.o -o prog3

clean:                                   [Clean up]
        rm -f *.o

Test case:

Get the following files: Makefile, prog1.f, prog2.c, sub2.c, prog3.f, and sub3.c.

Now issue the followings commands and NOTE what happens:

  1. make prog1 – you should see the g77 compiler used

  2. make prog2 – you should see the gcc compiler used on both prog2.c and sub2.c

  3. touch prog2.c and then make prog2. What is different? You will see that only prog2.c is recompiled since the touch command made the prog2.c file younger than the prog2.o file – make uses the time difference to recompile only what is required instead of everything. This is very useful if you have a big project. This example would simulate the fact the subc.c is well written and does what you want, while you make changes to prog2.c to improve it.

  4. make all compiles them all. test the programs by issuing the commands prog1 prog2 prog3

  5. Now do an ls to see what you have in your directories. After this enter make clean followed by an ls . What happened? For my software distribution my all: line is as follows:

all: prog1 prog2 prog3 clean

and the individual program lines are

prog3: prog3.o sub3.o                    
        $(FCMP) prog3.o sub3.o -o prog3
        mv prog3 $(DEST)

where the DEST variable points to the install directory, e..g., DEST=../bin


Last Changed September 22, 2006