next up previous contents
Next: Locally added software Up: No Title Previous: Transferring files from/to other

Compiling programs

Programming languages available

The following programming languages are available: FORTRAN (f77 is the compiler from Digital Equipment Corporation, f772.1 is the compiler from MIPS), C, gcc (GNU C compiler), perl (replacement for sed and awk), as (assembler for the MIPS R3000), p2c (a Pascal to C converter), f2c (a FORTRAN to C converter), web (TeX is written in web), web2c (a web to C converter).

FORTRAN

The workstation has two FORTRAN 77 compilers: f77 is written by DEC and is almost identical to the one on VAX/VMS machines; f772.1 is a compiler written by MIPS and is almost identical to the one on Silicon Graphics machines. Both compilers use IEEE standard floating point numbers, i.e., single precision ranges from tex2html_wrap_inline1307 to tex2html_wrap_inline1309 and double precision from tex2html_wrap_inline1311 to tex2html_wrap_inline1313 . There are no quadruple or higher precision floating point numbers.

The choice of compiler is up to you. In general, the f77 compiler produces more highly optimized code, but takes much longer to compile. The f772.1 compiler is a useful alternative, and is sometimes helpful in identifying potential errors in your program, for example, it will give you a warning message if you declare a local variable and then don't use it, and will also warn you if you declare the same common block in different subroutines with different lengths.

Recommendation: use f772.1 during the debugging stages when you may need to compile many times. Use f77 for the final production runs of programs where performance is critical. If your program has bugs that you are having difficulty tracking down with one compiler, try compiling it with the other one--this will often give you additional clues.

FORTRAN source files must end with the characters ``.f''. To compile and link a FORTRAN program you use

f772.1 prog.f
Note: in this and subsequent examples, you can substitute f77 for f772.1 if you wish to use the DEC compiler. The above command will place the executable binary file in the file a.out. To run the program, type

a.out

If there were any errors in the compilation, the file a.out may still be created, but will not have execute permission, so you can't run it. (If you really want to try running it, use chmod (see §gif) to change the file permissions). To rename the output file, either use the mv command, or use the -o switch on the f772.1 command, e.g.,

f772.1 prog.f -o prog

To compile a subroutine module without linking, use

f772.1 -c sub.f

which generates an object file called sub.o, which you can then link with the main program prog.f by using

f772.1 prog.f sub.o

A better way of compiling programs that consist of numerous subroutines in separate files is to use the UNIX make command. make is an ingenious program that takes a set of rules (stored in a file called Makefile) and uses them to build something. An example should make things clearer. Suppose that you have a program called prog with a main routine in prog.f and subroutines in f1.f, f2.f, and f3.f, then place the following text in Makefile with your favourite editor

prog:   prog.o f1.o f2.o f3.o
        f772.1 prog.o f1.o f2.o f3.o -o prog
Note: the blank space after prog:, and before f772.1 is a tab character, not multiple ASCII space characters. This is essential. The first line of the Makefile tells make that the target prog depends on the files prog.o, f1.o, f2.o, and f3.o. The second line tells make how to construct prog from these files. So if you type make your program will be compiled using these rules. The clever bit is that if you make a change to one of the input files, say f2.f, then when you type make again only f2.f will be recompiled to construct prog. make achieves this by looking at the modification times of the files, and only recompiling those files that have been modified since the creation of the associated object file.

Recommendation: when writing a new program, create a new directory for it, place all the relevant files there, and write a Makefile to handle the compilation. This will save you a lot of typing and reduce the number of unnecessary compilations.

Note that make is considerably more powerful than the above example would suggest. Unfortunately, the man documentation is probably the most opaque of any ever written, so your best way of learning how to use it is to examine other people's Makefiles, and a good place to do that is in the /usr/local/src directory tree.

Debugging FORTRAN programs

The DECstation comes with an excellent debugging program, called dbx. To use it you must have compiled your program with the -g -O0 switches, and it is probably also a good idea to use -check_bounds as well. For example,

f772.1 -g -O0 -check_bounds prog.f -o prog

You then run the program by typing

dbx prog

At this point you will be positioned at the first executable line of the program, and you can type ``r'' (for ``run'') to start the program. Using dbx you can set breakpoints (stop at 314, stop in mysub), examine variables (p myarray), step one line at a time (s), continue to the next breakpoint (c), find where the program has stopped (where), examine the source code at the current point (list), and so on (use help within dbx, or see the man pages, for more information). It is well worth spending the time learning how to use the debugger.

Note that when you have finished debugging your program, you should recompile the production version without the -g -O0 -check_bounds options, since otherwise you will be paying a large penalty in execution time.

Problems in porting FORTRAN programs

By its very nature FORTRAN is a difficult language to program reliably. In almost all cases, if your program doesn't work, its your fault not the computer or the compiler. To have any hope of eliminating bugs you must

For example, it is easy to write FORTRAN programs which work correctly on a VAX or an IBM 3090, but which fail on newt (and vice versa). A common problem is that the VAX REAL*4 type is identical to a truncated version of the VAX REAL*8 type, so that if you pass a REAL*8 argument to a subroutine that expects a REAL*4 it will appear to work on a VAX (although your accuracy will be reduced). Such a subroutine will fail (as is reasonable) on newt.

If you read/write to a file without specifying its name in the OPEN statement, then FORTRAN will refer to the file as fort.xx where xx is the unit number of the file. For example, the FORTRAN statement

        READ (10) I

will attempt to read from the file fort.10 in the current directory. If you want this to be another file, e.g., ../data/myfile.dat, then either use the NAME= parameter in an OPEN statement, or create a logical link to this file before running your program:

ln -s ../data/myfile.dat fort.10

A common problem with programs is that they fail with a ``bus error'' or ``segmentation violation''. This is usually caused by an attempt to read/write an area of memory that is not accessible to the program. For example, if you write to the 1001'st element of an array with only 1000 elements, then you may get a ``segmentation violation''. Similarly, if you call a subroutine with a fixed argument, as in

call mysub (12.34)

and if mysub attempts to change its argument, you may get all sorts of strange errors later on in your program.

If you want to write portable programs that have a high probability of being correct, then you need to program in a different language, such as PASCAL, ANSI standard C, MODULA-2, or MODULA-3. These languages prevent a lot of the problems associated with poor compile-time checking in FORTRAN.

The MIPS f772.1 compiler has a few tricks associated with its use. For example, the FORTRAN 77 standard doesn't specify whether the values of variables used locally in subroutines should remain fixed from one call of the subroutine to the next. If you want this behaviour (common with many other compilers) you will need to use a SAVE statement in the affected subroutines, or use the -static switch when compiling. If you want to access more than about half a megabyte of storage, it is essential that you make the arrays static (using SAVE or -static) rather than dynamic, otherwise the compiler places the arrays on the stack, which rapidly becomes full.

Also, in common with most modern RISC computers, the DECstation has constraints on the byte alignment of variables in COMMON blocks. For example, 8-byte quantities must be aligned on 8-byte boundaries, 4-byte quantities on 4-byte boundaries, and so on. In general, put the largest width variables first in COMMON block declarations. See the FORTRAN manual (accessible using dxbook) for details.

C

See the Physics page in xmosaic for C programming course notes written by Michael Ashley.

C source filenames must end with the characters ``.c''. The syntax used to call the compiler is similar to that for FORTRAN (in fact, the FORTRAN compiler converts your program into C and then calls the C compiler).

cc test.c -o test

You may also consider using the GNU C compiler gcc, which is a standard compiler available on many different computers and operating systems. Under some circumstances gcc will produce faster code than the default (MIPS) C compiler.

See the discussion on make in the section on FORTRAN programming.

Pascal

Pascal source files must end with the characters ``.p''. The Pascal compiler is called `pc'', and has similar semantics to the FORTRAN and C compilers. See `man pc' for details of the options. The Pascal compiler is not currently installed.

Standard mathematical libraries: NAG, IMSL, Numerical Recipies

NAG is available, to link with it simply add the -lnagf77 switch after your program sources files in the f77 command, e.g.,

f77 prog.f -lnagf77 -o prog

If using f772.1 do

f772.1 prog.f -lf77 -o prog

IMSL is not currently available, although it has been purchased by other Schools in the University. See Michael Ashley if you are interested. The source for Numerical Recipies is in /usr/local/src/recipies, however, the legal restrictions on Numerical Recipies are so severe that it is not allowable for anyone to use them.


next up previous contents
Next: Locally added software Up: No Title Previous: Transferring files from/to other

Michael C. B. Ashley
Fri Jun 28 13:34:23 EST 1996