CSU Logo
College of Engineering, Computer Science and Technology

Machines ] Editors ] Putty ] Bash Shell ] Lab Submission ][Java API]

   Thanks to Michael Long for this page.
         

Makefiles

A makefile is a file used by the Unix make utility.  Basically, the make utility will execute the script in the makefile.   This gives you the ability to build complex software with dependencies.  A simple makefile for your first homework assignment is depicted below.  You will need to type this in and put it in your public_html/111/lab1 directory.  From now on, you can modify this file to compile your labs.  I will go over how to create this file in lab and also in class at a later date.  For your later assignments, we will be creating a bit more complex makefile.

This file must be named either Makefile or makefile.

It is executed with the make command.  Just type the word make while in the public_html/111/lab1 directory.  Remember: cd ~/public_html/111/lab1

The make command will execute the first target name it finds, which is HelloWorld.class.  See below for an explanation of each line.  If at anytime you want to force a new compile of your source files, just delete the file HelloWorld.class.

1
2
3
4
5
6
7
8
9
10
#This is a comment.  It is ignored by the make utility.
CC=javac -d
DOC=javadoc -private -d _docs
HelloWorld.class : HelloWorld.java
[TAB]$(CC) HelloWorld.java 2> errorlog
[TAB]cp HelloWorld.java HelloWorld.txt
docs : HelloWorld.java
[TAB]$(DOC) HelloWorld.java

 Note that the indented lines have TAB characters (do not type the phrase [TAB], just hit the TAB key!).  There is no space after the TAB.  This is a requirement of the makefile format.

Line 1 Comment, completely ignored by make.
Line 2 Define a macro named CC to run the javac command.
Line 3 Define a macro named DOC to run the javadoc command.
Line 4 Separate the sections with a blank line.
Line 5 HelloWorld.class is a target.  If HelloWorld.java has not been modified since the last HelloWorld.class file was created, make will exit here.  If it has changed since the last good compile, make will continue processing this makefile.
Line 6 Run the CC macro to compile the HelloWorld.java file and send any error output to a file named errorlog.  If there is no error output, the file errorlog will be empty.  If there is an error, the make utility will stop processing here and the errorlog file will contain the error output of the compiler.  The 2> tells the Unix shell to send error output to the file indicated (errorlog).
Line 7 If the compile is successful this line will copy the HelloWorld.java file to HelloWorld.txt as a backup.
Line 8 Separating blank line.
Line 9 docs is a target.  It depends on the HelloWorld.java file existing.  Typing  make docs will cause this target to be processed.  This target cannot have the same name as the _docs directory.  This is why I chose to have the _docs directory name begin with an _ character.
Line 10 Run the DOC macro to create javadoc web pages for the lab.  These will be stored in the _docs directory.  (public_html/111/lab1/_docs).

To compile your program just type make

To create javadocs, just type make docs

Always make docs as the last step before turning in a project.


For more on makefiles, see GNU make, The Makefile or makefile Conventions