Programming on Linux Machines
This chapter describes how to compile and run a program on the UNIX computers in the Mathematics Department. It includes a discussion of online libraries of functions and subroutines that you may use.
There are three steps in creating an executable program. First, one must type in the code, that is, the FORTRAN or C text that is processed by a compiler. Next, one compiles this into object code. Finally, one links the various files containing object code together, assigning storage to variables and so on. The terms in italics will be used repeatedly in this text.
The department has a variety of programming languages for you to use. The old standby FORTRAN 77 is available on virtually all UNIX computers. To use it to compile a file, first name the file using a .f extension. For example one might try to compile the file test.f as follows.
delta> f77 test.f
As usual, delta> is just the prompt - you do not have to type that. By naming the file using the .f extension, the compiler knows that it should compile the file, and then link it. If the compilation and linking is successful, then a file a.out appears in the current directory. This is the executable file. It may be executed simply by typing a.out.
If you are using C, the command is exactly analogous:
delta> cc test.c
Again, the compiled program is named a.out, and you execute it by typing its name. On most computers we use the Gnu C compiler, which has C++ built-in. Thus, you may compile either a C program or a C++ program using
delta> gcc test.c
or for C++ in particular,
delta> g++ test.c
Sometimes, if your path variable is misconfigured, then the computer will not recognize a.out as a command. If this happens, you may execute the file by typing
Most of the time it is desirable to be able to give a name to the executable file. To give the name test to the executable file for test.f , type
delta> f77 test.f -o test
Likewise if you are using C or C++, then use the command
delta> gcc test.c -o test
Often it is useful to place code for a given program in several files, e.g. test1.f, test2.f, and test3.f. Doing so allows one to compile just one of the files at a time, and create an object file. Object files cannot be executed - they are compiled, unlinked files. Object files always have a .o extension. The following commands compile the file test1.x and creates the object file test1.o, but does not attempt to create an executable file, for x = f and x = c, respectively.
delta> f77 -c test1.f
delta> gcc -c test1.c
If a file with a .o extension appears on the f77 line, then the compiler assumes that the file has already been compiled and is ready to be linked. The following command compiles the file test2.f, and then links the resulting object file with test1.o and test2.o to create the executable file test.
delta> f77 test1.o test2.f test3.o -o test
There are a number of options for the f77 and cc commands that allow one to control almost every aspect of the compilation and linkage of a file. See the man pages for f77 or cc. One may create arbitrarily complicated compilation structures using the make utility, but that utility is beyond the scope of this document. Again, see the man pages on make.
A FORTRAN 90 compiler, called f90, is available on all Tru64 computers (thetahat, mazama, etc.) as well as on thetabar.
There are many libraries available to enhance the capabilities of compiled programming languages. C, in particular, provides a large number of libraries to facilitate mathematical and systems programming. Library references are loaded with programs at compile time using the -l flag. For example, to use the math library with a C program, type the line
theta> cc prog.c -o prog -lm
For further information concerning the library you want to use see the man pages for the compiler you are using, e.g.
theta> man gcc