Index->Programming ToolsProgramming Tools
Overview and Goals
At the completion of this lab, you will be able to...
- Run a Java program from the command line.
- Compile a Java program from the command line.
- Create a Makefile to automatically compile source code.
- Use touch to change the time-stamp of a file.
- Pretty print a source code
- Perform basic version control operations on your source files.
The following utilities are used in this lab:
java(1),
javac(1),
make(1),
rcs(1),
touch(1),
enscript(1),
java and javac
Compiling and running a Java program on the command-line is quite
easy. For this lab, create a small "Hello, World" program in Java
using your favorite editor. We'll assume you call this file
Hello.java for the rest of this tutorial.
To compile your program, simply type
javac Hello.java
at the command line. javac is the Java compiler.
It takes your Java source code and produces the bytefile
"Hello.class".
If your original Hello.java had no syntax errors, go back
into your editor and introduce some. Try recompiling and look at the
kinds of error messages that you see. Notice that javac
reports the line on which the error occurred, so you can go and
correct the error.
Once you have the .java file compiled, you can run it on
the command line with the java program. The format is
"java classname". In our example, you would type
java Hello
NB: You do not put the ".class" extension. It is assumed.
make
If you have lots of source files, then recompiling all of them is
both unnecessary and time consuming. In order to run your program, you
only need to recompile those source files that have changed. Source
files that are not changed don't need to be recompiled. For small
projects, keeping track of changes to files may be easy, but as the
number of source files grow, this becomes more of a challenge.
make to the rescue.
Using one of the internet search engines (Google, Lycos, ...), look
for "Introduction to make" (type the quotes as well in the search text
box). Use one of the links to give you a start on using Makefiles.
Using a good introductory document found from the search, you should get
a good feeling for how make works. make determines
which source files have been changed, and only recompiles those
files. A more complete documentation of make can be found by typing
"info make".
Create a Makefile for your "Hello, World" project. Here's
a sample Makefile. Be sure you understand what it's doing:
Hello.class : Hello.java
javac Hello.java
clean :
rm -f *.class
Note: make sure you have a TAB character on each command line (the
line after the target line) in your Makefile.
Once you have the Makefile completed, you can compile your
system simply by typing make on the
command-line. Since "Hello.class" is the default target
(because it is the first target listed in the Makefile), it will be
re-created, if it's source file has been changed.
With larger projects, with many source files, you would have a rule
for each .java that compiles it into a .class file.
(In reality, you would use a suffix rule to provide one rule
for all of your .java files).
You should be aware that make does not actually keep track
of changes, it only keeps track of timestamps on files. Every file
under Linux has a time of last modification. It is the time shown
when you use "ls -l". make simply looks at the
timestamps of the source and compiled file. If the source file has
been modified more recently than the compiled file, then it will
recompile the source.
Because make only cares about timestamps, you can force
make to recompile a file simply by changing the time of last
modification of a source. You could do this by opening the source in
an editor, making a small, but insignificant change, then saving the
file again. An easier way is to use the touch utility.
touch changes the time of last modification of a file to the
current time. For example, if you type touch
Hello.java the time-stamp of Hello.java
will be changed, even though the file's contents have not. Using
touch allows you to force make to recompile your
files.
g++ and gcc
Two other commonly used languages are C++ and C. Compiling C++ and
C programs on the command-line is fairly easy. C++ programs are
compiled using the g++ compiler, while C programs are
compiled using the gcc compiler. Both compilers work
identically, and produce an object file ".o". Both of them
can also be used as a front-end to the linker (ld) to produce
an executable program.
Compiling a single file into an object file is done with
"gcc -c sourcFile". The "-c" switch says to
"compile only" (i.e., don't produce an executable). When the compiler
is finished you will be left left a ".o" file.
Producing an executable is done with the "-o" option. The
format is "gcc -o executableName
[sourceFile(s)|objectFile(s)]". For example
"gcc -o runme main.c io.c" would create the executable
"runme" by compiling and linking the two source files
"main.c" and "io.c".
Printing Java, C, C++, ... Programs
In previous lab you learned that you can use lpr to print your Unix
file. I discourage you from plainly using lpr to print your source codes
for the following reasons:
- lpr does not add page number to your pages. If your printout were
scattered on your desk, it is difficult to put them back in the right
order
- lpr cannot do pretty printing
One tools that you can use to pretty print your source codes is
enscript(1).
Enscript provides a number of input filters that you can use to
pretty print your code. To obtain the list of available filters, run the
following command:
enscript --help-pretty-print
Once you know the name of the filter to use, you can pass it to the
-E or --pretty-print option. In our installation,
there are more than 30 filters available, some of them are:
asm, c, cpp, fortran, html, java, javascript, makefile, pascal, perl.
For instance, to print a Java program using enscript you will type:
enscript -G -Ejava your_program.java
Revision Control System (RCS)
For projects where you are doing a lot of incremental development,
you may want to use RCS to keep track of changes to your files. RCS
is a version control system, meaning that it keeps track of all the
changes you make to each of your source files. RCS does this by using
a library metaphor. You "check-out" a file, make changes, then
"check-in" the updated file.
RCS is useful because you can recreate an old version of a file.
For instance, if you made some changes to a file, then decided that
you really didn't want to do it anyway, you can have RCS give you an
earlier version of the file.
Create a directory "RCS" in the directory with your
"Hello.java" file. Then check-in your file with "ci
Hello.java". You will be prompted to describe the file. Once
you do that, notice that Hello.java has disappeared from the
directory. It now lives in the RCS repository. To get a read-only
copy, just check it out "co Hello.java". To check out a
version that you can edit, you must "lock" out others from checking
out the same file. You do this by simply using the "-l"
option to co: "co -l Hello.java". Now you can make
changes to the file.
Make a change or two and check the file back in. RCS will prompt
you for a description of the changes that you made.
Other useful RCS tools include rlog to see the change
history of a file, and rcsdiff to see the actual differences between
two version of the file.
Question
Turnin your answers to the following questions:
- Create a Makefile for compiling your Java program (modify the
example given above if necessary). Now run "cat Makefile" and
"cat -A Makefile". Explain the difference of the output.
- List all the rules you have in your Makefile? What is the name of
the target in each rule?
- Describe the command to generate each target?
- Read the manual page of cat and explain how the -A
option changed the output. What does the -T option do?
- Using the --help-pretty-print option, find out three more filters
for programming language supported by enscript.
- Pretty print your java source code using enscript.
- Run the touch command on a
non-existant file, and then
run "ls -l" on that file name. What do you get?
|
Top
|