NERSC logo National Energy Research Scientific Computing Center
  A DOE Office of Science User Facility
  at Lawrence Berkeley National Laboratory
 

Simple make

We'll use make in its most simple forms to produce an executable program from these source code files.

Simple compile

In order to create an executable program, all these source code files need to be compiled and linked. This command line will do it:

% xlf90 -o project project_info.f print_project_info.f project.f 

That's a lot of typing even for this simple program. make will save you from having to remember and type the command line each time.

The make utility gets its instructions from a text file, by default named makefile or Makefile in the current directory.

Here is possibly the simplest and clearest makefile that will perform the above compile and link:

#
# A first makefile

project : project_info.f print_project_info.f project.f
	xlf90 -o project project_info.f print_project_info.f project.f

The pound character, #, in a makefile indicates a comment line. A backslash character indicates that the line is continued on the next line. If we save these lines in a file named makefile, typing make at the command line will compile the program just as if we'd typed in the entire f90 command:

%  make 
project_info.f:
** project_info   === End of Compilation 1 ===
1501-510  Compilation successful for file project_info.f.
print_project_info.f:
** print_project_info   === End of Compilation 1 ===
1501-510  Compilation successful for file print_project_info.f.
project.f:
** project   === End of Compilation 1 ===
1501-510  Compilation successful for file project.f.

In addition, if we don't change any files and type make again:

%  make 
Target "project" is up to date.

make knows that the file does not need to be recompiled. However, if we change one of the source code files, make will recompile the program.

How does it know how to do this? Let's look at the makefile. The first line,

 
project : project_info.f print_project_info.f project.f

contains the program name, project, to the left of a colon. The word or label to the left of the colon is called the target. The files that appear to the right of the colon are called dependencies. The dependencies are those files that are needed to produce the target. So this line is telling make that all the source code files are needed to produce the executable program.

make checks the dependency list and if any of the files have been changed more recently than the target, make executes the next line in the makefile:

    xlf90 -o project project_info.f print_project_info.f project.f

If the target is more recent than the dependency, make will tell you so and skip the command.

NOTE: Although you can't see it, there is a TAB character before the xlf90 command. This TAB must be present for make to work. All the blank spaces in the world will not help if there is no TAB present and make will give you little clue as to why it is failing.

Better compile

In the previous example, every source code file was recompiled every time a new executable was produced. This is not necessary in general, and it could waste a lot of time. If we can understand the steps xlf90 takes to build the program, we can instruct make to work in the most efficient manner.

The xlf90 compiling system works in two stages; it:

  1. compiles each .f file into an object file ending with a .o extension
  2. links all the .o files and system library files into the executable program

All that is needed to produce the final program are .o files. The .f files are needed to create .o files. (A possible subtlety regarding xlf90: if a .f file USEs a module contained in a different .f file, it reads the necessary information from the module's .mod file, which must exist.)

Here are the dependencies for this program:

This file's creation depends on these files
project_info.o project_info.f
print_project_info.o project_info.o print_project_info.f
project.o project.f
project project.o print_project_info.o

Here's the makefile that incorporates all these dependencies:

project :  project.o print_project_info.o
        xlf90 -o project project.o print_project_info.o

project_info.o : project_info.f
        xlf90 -c project_info.f

print_project_info.o : print_project_info.f project_info.o
        xlf90 -c project_info.o print_project_info.f

project.o : project.f
        xlf90 -c project.f

The xlf90 -c command tells the compiler to produce the corresponding .o file without trying to link it into an executable.

Now let's make the program:

%  make 
        xlf90 -c project.f
** project   === End of Compilation 1 ===
1501-510  Compilation successful for file project.f.
        xlf90 -c project_info.f
** project_info   === End of Compilation 1 ===
1501-510  Compilation successful for file project_info.f.
        xlf90 -c project_info.o print_project_info.f
** print_project_info   === End of Compilation 1 ===
1501-510  Compilation successful for file print_project_info.f.
        xlf90 -o project project.o print_project_info.o

Typing make again produces:

 % make 
Target "project" is up to date.

Running the program on the NERSC SP yields

%  ./project 
 This is PROJECT version 0 . 2

Now let's change the version to 0.3. After we have edited the file project_info.f, we rebuild the program:

% make 
        xlf90 -c project_info.f
** project_info   === End of Compilation 1 ===
1501-510  Compilation successful for file project_info.f.
        xlf90 -c project_info.o print_project_info.f
** print_project_info   === End of Compilation 1 ===
1501-510  Compilation successful for file print_project_info.f.
        xlf90 -o project project.o print_project_info.o
% ./project 
 This is PROJECT version 0 . 3

Note that make only executed the steps that were necessary to build the program. In particular, project.f was not recompiled. print_project_info.o was recreated because it contains information that is defined in project_info.f.


LBNL Home
Page last modified: Fri, 21 May 2004 22:28:00 GMT
Page URL: http://www.nersc.gov/nusers/help/tutorials/make/simple_make.php
Web contact: webmaster@nersc.gov
Computing questions: consult@nersc.gov

Privacy and Security Notice
DOE Office of Science