Compute Canada

How do I compile and link programs? Which compiler flags should I use?

Please note: The FAQ pages at the HPCVL website are continuously being revised. Some pages might pertain to an older configuration of the system. Please let us know if you encounter problems or inaccuracies, and we will correct the entries.

Normally, you use the "Sun Studio" compilers in /opt/SUNWspro/bin to compile and link. To compile a Fortran 77, Fortran 90, Fortran 95, C, or C++ program, you issue the f77, f90, f95, cc, or CC commands, respectively. Compiling and linking is best done with a makefile. But you can also issue the commands by hand.

To compile:

 compiler -c [options] name.ext

compiler = f77, f90, f95, cc or CC; name = name of your program source file; ext =extension, i.e. f (fixed format) or f90 (free format) for Fortran (90), c for C, cpp or C for C++, etc., [options] denotes compiler flags that usually start with an '-')
Note for Fortran programmers: You are actually using the Fortran90 (f90) compiler even if you are compiling F77 programs. The f77 command issues additional compiler flags that concern compatibility.

To link:

 compiler -o name [options] [libraries] list 

(compiler see above; name name of the executable; [options] see above; [libraries]libraries that need to be linked in, usually as a list of file names with full path, or as '-L' and '-l' combinations [see below]; list list of object files, usually with .o extension)

Using the compilers and the linker in the above manner requires the proper setting of thePATH environment variable.

There are hundreds of compiler flags, and many of them are not required most of the time. A few that are in more frequent use are:

-xOn optimizes your code. nis a number from 1 to 5 with increasing severity of alterations made to the code, but also increasing gain. Up to -xO3 is generally rather safe to use. But you should, of course, always check results against an un-optimized version: they might differ.

-fast is a combination of optimization flags that is quite safe to use and often improves performance a lot. However, the resulting code is optimized specifically for the current machine architecture and cannot be executed on older SUN's (including the UltraSparc-III). Note that this overrides the -xOn option if it comes after it, since compiler options are executed from left to right! If you use this flag for compiling, you also need to include it at the linking stage.

-g produces code that can be debugged. Unlike for other compilers, -g and -xOn are not mutually exclusive, so it is a good flag to have in the development stage of a program.

-v produces more output than you can handle, which makes it easier to track down problems.

-lname is used to bind in a library called libname.a (static) or (dynamic). This flag is used to link only.

-Ldirname is used in conjunction with -lname and lets the linker know where to look for libraries. dirnameis a directory name such as /opt/studio12/SUNWspro/prod/lib.

-Rdirname is used to tell the program where to get dynamic libraries at runtime.

There are many more flags. They are documented in the man pages (man f90 or man cc), as well in various documents that may be downloaded in pdf format from the Sun documentation website. The latter is a good place to look to resolve problems in any case. Use the search engine to obtain User's Guides and Reference Manuals.

Some compiler flags are only useful for parallel programs and will be discussed later. Sometimes there is a considerable performance gain from using specific options (such as-xchip and -xtarget), but the code becomes less general.