Compilers

by Anu Rao


Table of Contents


Introduction

There are several compilers on our SUN system here at the Math Dept. We shall focus on the C and Fortran compilers available on the SUN system. In general, compilers translate (compile/link) programs we write in C/Fortran into binary representations that the computer can understand and execute.
[Back]

C Compilers

Three commonly-used C compilers on our system are acc, cc, and gcc. The cc compiler is the Kernighan & Ritchie (K&R)-style C compiler, whereas acc compiles ANSI C code. K&R and ANSI C differ significantly, and if you try to use the wrong compiler for your code you may be bombarded with error messages. Make sure you know which type of C you are programming in and use the corresponding compiler.

In addition we have gcc, the GNU C compiler which can compile both K&R and ANSI C programs. The advantage of using gcc is that it is pretty much uniform across architectures (SUN, LINUX, Slowaris, DEC, etc.), so if you write code that will compile and run with gcc on the SUNs, chances are that it will compile and run on, say, a Linux machine using gcc. This is not necessarily the case with acc and cc.

All three compilers have several "options" or "flags", which are of the form -flagname. These flags allow us to turn on (or off) various options of the compiler in question. All the flags are described in the man pages. We will see several examples of flags below.

[Back]

Simple Compilation

Let's say you have a C program named goof.c (all the C compilers listed above assume the suffix ".c" for C code). If the code is in ANSI C you can use either

    acc goof.c
    or
    gcc -ansi goof.c

If the program is in K&R C you can use either

    cc goof.c
    or
    gcc goof.c

Any/all of these commands will compile/link your program goof.c and produce a file called a.out (the executable version of your program goof.c). Now to run your program, at the Unix prompt type

    a.out

If you are compiling several different programs in one directory, then having them all named a.out could be disastrous! In general it is a good idea to add the -o name option, which writes the executable file to name instead of a.out. So to compile our program goof.c and give the executable the name goof, we would type

    acc goof.c -o goof

The -o name option works with acc, cc, and gcc.

[Back]

Including Libraries with C code

We can also link our code to include libraries. A library is precompiled code archived together in one file for convenience; it is in binary form and usually has the prefix "lib" and suffix ".a"). Some examples of libraries on our system are in the directory /usr/local/lang/SC1.0. To include a library libname.a we would type

    acc goof.c -o goof -lname

The -lname option works with acc, cc, and gcc.

[Back]

Optimization and Debugging Flags

Optimization flags help your code compile/run more efficiently, sometimes up to 2-3 times faster! There are several optimizing flags for all three C compilers. Since optimizations apply to hardware as well as software, they can vary widely from architecture to architecture (i.e., from SUN to SGI to DEC), so be sure to check the man pages of your particular C compiler.

Here are some commonly used optimizaton flags for the various C compilers on the SUNs:

For more details and information on what combinations of optimizers work well together, consult the man pages.

To use debuggers such as xdbx or gdb, you need to compile with the -g option (works with acc, cc, and gcc).

In addition, gcc has some great flags to help find bugs/errors. In particular, the -Wall is very useful. What does it do? According to the gcc man page.....

 -Wall
          Issue warnings for conditions which  pertain  to  usage
          that  we recommend avoiding and that we believe is easy
          to avoid, even in conjunction with macros.
[Back]

Fortran Compilers

The two most-used Fortran compilers on our system are f77 and g77. The f77 compiler is the SUN Fortran 77 compiler, and g77 is the GNU version of the Fortran 77 compiler.

As with the gcc compiler the advantage of g77 is its uniformity across different architectures, as the f77 compiler can vary wildly from SUNs to DECs to SGIs, etc., etc..

Both compilers have several "options" or "flags", which are of the form -flagname. These flags allow us to turn on (or off) various options of the compiler in question. All the flags are described in the man pages. We will see several examples of flags below.

[Back]

Simple Compilation

Let's say you have a Fortran program named goof.f (both the Fortran compilers listed above assume a ".f" suffix for Fortran code). To compile goof.f you can use either

f77 goof.f
or
g77 goof.f

Both of these commands will compile/link your program goof.f and produce a file called a.out (the executable version of your program goof.f). Now to run your program, at the Unix prompt type

a.out

If you are compiling several different programs in one directory, then having them all named a.out could be disastrous! In general it is a good idea to add the -o name option, which writes the executable file to name instead of a.out. So to compile our program goof.c and give the executable the name goof, we would type

f77 goof.f -o goof

The -o name option works with both f77 and g77.

[Back]

Including Libraries with Fortran code

We can also link our code to include libraries. A library is precompiled code archived together in one file for convenience; it is in binary form and usually has the prefix "lib" and suffix ".a"). Some examples of libraries on our system are in the directory /usr/local/lang/SC1.0. To include a library libname.a we would type

f77 goof.f -o goof -lname

The -lname option works with both f77 and g77.

[Back]

Optimization and Debugging Flags

Optimization flags help your code compile/run more efficiently, sometimes up to 2-3 times faster! There are several optimizing flags for both Fortran compilers. Since optimizations apply to hardware as well as software, they can vary widely from architecture to architecture (i.e., from SUN to SGI to DEC), so be sure to check the man pages of your particular Fortran compiler.

Some commonly used optimizaton flags for f77 on the SUNS are -Bstatic, -cg89, -O2, -fast, -fnonstd, -libmil, O2. One particular optimization flag for g77 is -O. For more details and information on what combinations of optimizers work well with each other, consult the man pages.

To use debuggers such as xdbx or gdb, you need to compile with the -g option (works with both f77 and g77).

In addition, g77 has some great flags to help find bugs/errors. In particular, the -Wall is very useful. What does it do? According to the g77 man page.....

 -Wall
          Issue warnings for conditions which  pertain  to  usage
          that  we recommend avoiding and that we believe is easy
          to avoid, even in conjunction with macros.
As you will discover from its man page, g77 has a barrage of options, some of which are helpful and some that are not. You will need to sort through them to determine which flags sound useful for your purposes.
[Back]
http://math.arizona.edu/~swig/documentation/compilers/index.php
Last modified: Fri, 14 Dec 2007 15:50:51 -0700
E-mail: swig@math.arizona.edu
Valid XHTML 1.0! Valid CSS!