Chapter 1. Compiling, Linking, and Running Programs

This chapter provides an overview of the MIPSpro F77 compiler and its use. It contains the following major sections:

Compiling and Linking

This section discusses compilation and linking issues when using the compiler.

The format of the f77 command is as follows:

f77 [option] ... filename.[suffix]

The options argument represents the options you can use with this command. See the f77(1) man page for a complete description of the options and their use.

The filename.suffix argument is the name of the file that contains the Fortran source statements. The filename must always have the suffix .f, .F, .for, .FOR, or .i. For example, myprog.f.


The f77 command can both compile and link a source module. Figure 1-1 shows the primary compilation phases. It also shows the principal inputs and outputs for the source modules more.f.

Figure 1-1. Compilation Process

  • The source file ends with the required suffixes .f, .F, .for, .FOR, or .i.

  • The Fortran compiler has an integrated C preprocessor that provides full cpp capabilities.

  • The compiler produces a linkable object file when you specify the -c command option. This object file has the same name as the source file, but the suffix is changed to .o. For example, the following command line produces the more.o file:

    % f77 more.f -c   

  • The default name of the executable object file is a.out. For example, the following command line produces the executable object a.out:

    % f77 myprog.f

  • You can specify a name other than a.out for the executable object by using the -o name option, where name is the name of the executable object. For example, the following command line links the myprog.o object module and produces an executable object named myprog:

    % f77 myprog.o -o myprog   

  • The following command line compiles and links the source module myprog.f and produces an executable object named myprog:

    % f77 myprog.f -o myprog   

Compiling in C/C++

The compiler system uses drivers that allow you to compile programs for other programming languages, including C and C++. If one of these drivers is installed in your system, you can compile and link your Fortran programs to the language supported by the driver. See the MIPSpro Compiling and Performance Tuning Guide for a list of available drivers and the commands used with them. See Chapter 3, “Fortran Program Interfaces”, for information about the conventions you must follow when writing Fortran program interfaces to C programs.

When your application has two or more source programs written in different languages, you should compile each program module separately with the appropriate driver and then link them in a separate step. Create objects suitable for linking by specifying the -c option, which stops the driver immediately after the assembler phase.

The following two command lines produce linkable objects named main.o and rest.o, as illustrated in Figure 1-2.

% cc -c main.c
% f77 -c rest.f   

Figure 1-2. Compiling Multilanguage Programs

Linking Objects

You can use the f77 command to link separate objects into one executable program when any one of the objects is compiled from a Fortran source. The compiler recognizes the .o suffix as the name of a file containing object code suitable for linking and it immediately invokes the linker. The following command links the object created in the last example:

% f77 -o myprog main.o rest.o   

You can also use the cc command, as shown in this example:

% cc -o myprog main.o rest.o -lftn -lm   

Figure 1-3 shows the flow of control for this link.

Figure 1-3. Linking

Both f77 and cc use the C link library by default. However, you must specify them explicitly to the linker using the cc -l option as shown in the previous example. The characters following -l in the previous example are shorthand for link library files described in the following table.



library ftn in /usr/lib*/nonshared/libftn.a

Intrinsic function, I/O, multiprocessing, IRIX interface, and indexed sequential access method library for nonshared linking and compiling

library ftn in /usr/lib*/

Same as above, except for shared linking and compiling (this is the default library)

library m in /usr/lib*/

Mathematics library

See the FILES section of the f77(1) reference page for a complete list of the files used by the Fortran driver. Also see the ld(1) man page for information on specifying the -l option.

Specifying Link Libraries

You may need to specify libraries when you use IRIX system packages that are not part of a particular language. Most of the man pages for these packages list the required libraries. For example, the getwd(3B) subroutine requires the BSD compatibility library libbsd.a. Specify this library as follows:

% f77 main.o more.o rest.o -lbsd   

To specify a library created with the archiver, type in the pathname of the library as shown below.

% f77 main.o more.o rest.o libfft.a   

Note: The linker searches libraries in the order you specify. Therefore, if you have a library (for example, libfft.a) that uses data or procedures from -lm, you must specify libfft.a first.

Compiler Options: an Overview

This section contains an overview of the Fortran-specific compiler options, such as those that specify input/output files, or specify source file format. For complete information about the compiler options, see the f77 man page. You can also see the following documents for further information:

  • The MIPSpro Compiling and Performance Tuning Guide for a discussion of the compiler options that are common to all MIPSpro compilers.

  • The apo(1) man page for options related to the parallel optimizer.

  • The ld(1) man page for a description of the linker options.

    Tip: The f77 -help command lists all compiler options for quick reference. Use the -show option to direct that the compiler document each phase of execution, showing the exact default and nondefault options passed to each.

  • The lno(5) man page, for details about the Loop Nest Optimizer.

  • The opt(5) man page, for details about optimization options to the compiler.

Compiling Simple Programs

You need only a few compiler options when you are compiling a simple program. Examples of simple programs include the following:

  • Test cases used to explore algorithms or Fortran language features

  • Programs that are mainly interactive

  • Programs with performance that is limited by disk I/O

  • Programs that will execute under a debugger

In these cases you need only specify the -g option for debugging, specify the target machine architecture, and specify the word-length. For example, to compile a single source file to execute under dbx, you could use the following commands.

f77 -g -mips4 -n32 -o testcase testcase.f
dbx testcase

However, a program compiled in this way takes little advantage of the performance features of the machine. In particular, its speed when doing heavy floating-point calculations will be far slower than the machine capacity. For simple programs, however, that is not important.

Using a Defaults Specification File

You can set the Application Binary Interface (ABI), instruction set architecture (ISA), and processor type without explicitly specifying them. Set the COMPILER_DEFAULTS_PATH environment variable to a colon-separated list of paths indicating where the compiler should check for a compiler.defaults file. If no compiler.defaults file is found or if the environment variable is not set, the compiler looks for /etc/compiler.defaults. If this file is not found, the compiler uses the built-in defaults.

The compiler.defaults file contains a -DEFAULT:option group command that specifies the default ABI, ISA, and processor. The compiler issues a warning if you specify anything other than -DEFAULT: option in the compiler.defaults file.

The format of the -DEFAULT: option group specifier is as follows:

-DEFAULT:[abi=abitype] [:isa= isatype] [:proc=rtype] [:opt=level] [:arith=number] 

See the f77(1) man page for an explanation of the arguments and their allowed values.

Specifying Features to the Compiler

There are several specific features which you may wish to use as you compile your programs. The following tables discuss these features:

  • target machine features

  • source file formats

  • input and output files

  • memory allocation and alignment

  • debugging and profiling

  • optimization levels

  • multiprocessing options

  • recursion options

  • compiler execution options

The following options are used to specify the characteristics of the machine where the compiled program will be used.

Machine Characteristic Options



-64, -n32, -o32

Specifies that the target machine runs 64-bit mode, “new” 32-bit mode available with IRIX 6.1 and above, or old 32-bit mode. The -64 option is allowed only with the -mips3 and -mips4 architecture options.

-mips3, -mips4

The instruction architecture available in the target machine. Use -mips3 for MIPS R4000 and R4400® machines; use -mips4 for MIPS R8000, R10000™ and R12000 machines.


Specify certain details of the target CPU. Many of these options have correct default values based on the preceding options.


Specify certain details of the software environment where the source module will execute. Most of these options have correct default values based on other, more general values.

The following options specify the source file format for files used by the compiler.

Source File Format Options




Report any nonstandard usages.


Treat \ in character literals as a character, not as the start of an escape sequence.

-col72, -col120, -extend_source, -noextend_source 

Specify margin columns of source lines.


Compile lines with D in column 1.

-Dname -Dname=def, -Uname 

Define/undefine names to the integrated C preprocessor.

The following options direct the compiler to use specific input files and to generate specific output file.

Input/Output File Options




Generate a single object file for each input file; do not link.


Run only the macro preprocessor and write its output to standard output.

-I, -Idir, -nostdinc

Specify location of include files.


Request a listing file.


Request Makefile dependency output data.


Specify name of output file.


Specify only assembly-language source output.

The following options direct the compiler how to allocate memory and how to align variables in memory. These options can affect both program size and program speed.

Memory Allocation Options




Align all variables of size n on n-byte address boundaries. Valid values for n are 6, 8, 32, or 64.

When using 32, objects larger than 32 bits can be aligned on 32-bit boundaries. 16-bit objects must be aligned on 16-bit boundaries, and 32-bit objects must be aligned on 32-bit boundaries.


Specify the size of DOUBLE and DOUBLE COMPLEX variables.


Specify the size of INTEGER and LOGICAL variables.


Specify the size of REAL and COMPLEX variables.


Allocate all local variables statically, not dynamically on the stack.

The following option directs the compiler to include more or less extra information in the object file for debugging.

Debugging Option




Leave more or less symbol-table information in the object file for use with dbx or Workshop Pro cvd.

The following optimization options are used to communicate to the different optimization phases. The optimizations that are common to all MIPSpro compilers are discussed in the MIPSpro Compiling and Performance Tuning Guide.

Optimization Options




Select basic level of optimization, setting defaults for all optimization phases. level can be a value from 0 to 3.


Standalone inliner option group to control application of intra-file subprogram inlining when interprocedural analysis is not enabled. See the ipa(5) man page for more information


Specify Inter-Procedural Analyzer option group to control application of inter-procedural analysis and optimization, including inlining, common block array padding, constant propagation, dead function elimination, alias analysis and others.


Loop nest optimizer (LNO) option control group to control optimizations and transformations performed by LNO. See the LNO(5) referece page for more information.


Specify miscellaneous details of optimization. See the OPT(5) man page for more information.


Request execution of the parallelizing optimizer.

In addition to optimizing options, the compiler system provides other options that can improve the performance of your programs:

  • Two linker options, -G and -bestG, control the size of the global data area, which can produce significant performance improvements. See the MIPSpro Compiling and Performance Tuning Guide, and the ld(1) man page for more information.

  • The linker's -jmpopt option permits the linker to fill certain instruction delay slots not filled by the compiler front end. This option can improve the performance of smaller programs not requiring extremely large blocks of virtual memory. See the ld(1) man page for more information.

The f77 compiler has several options related to multiprocessing. However, the associated programs and files are not present unless you install Power Fortran 77. The following list summarizes some of the multiprocessing options:

Multiprocessing Options




The multiprocessing options group enables or disables multiprocessing features. All of the features are enabled by default with the -mp option. The following are the individual controls in this group:

dsm=flag. Enables or disables data distribution. flag can be either ON or OFF. Default: ON.

clone=flag. Enables or disables auto-cloning. flag can be either ON or OFF. The compiler automatically clones procedures that are called with reshaped arrays as parameters for the incoming distribution. However, if you explicitly specify the distribution on all relevant formal parameters, then you can disable auto-cloning with -MP:clone=off. The consistency checking of the distribution between actual and formal parameters is not affected by this flag, and is always enabled. Default: ON.

check_reshape=flag. Enables or disables generation of the runtime consistency checks across procedure boundaries when passing reshaped arrays (or portions thereof) as parameters. flag can be either ON or OFF. Default: OFF.

open_mp=flag. Enables or disables compiler to use OpenMP directives. flag can be either ON or OFF. Default: ON.

old_mp=flag. Enables or disables recognition of the PCF directives. flag can be either ON or OFF.


Enable the multiprocessing and DSM directives. Use this option with either the -mp or the -apo option. The saved file name has the following form:


If the TMPDIR environment variable is not set, then the file is in /tmp.


Keep the compiler generated temporary file and generate correct line numbers for debugging multiprocessed DO loops.


Run the apo(1) preprocessor to automatically discover parallelism in the source code. This also enables the multiprocessing directives. This is an optional software product.

Note: Under -mp compilation, the compiler silently generates some bookkeeping information under the rii_files directory. This information is used to implement data distribution directives, as well as perform consistency checks of these directives across multiple source files. To disable the processing of the data distribution directives and not generate the rii_files, compile your program with the -MP:dsm=off option.

You can enable recursion support by using the -LANG:recursive=ON option.

In either mode, the compiler supports a recursive stack-based calling sequence. The difference is in the optimization of statically allocated local variables. The following list describes the -LANG:recursive= option:

Recursion Options

Recursive Option



A statically allocated local variable can be referenced or modified by a recursive procedure call. The statically allocated local variable must be stored in memory before making a call and reloaded afterward.


The default. The compiler can safely assume a statically allocated local variable will not be referenced or modified by a procedure call and can optimize more aggressively.

The following options control the execution of the compiler phases.

Compiler Execution Options



-E, -P 

Execute only the integrated C preprocessor.


Stop compilation immediately after the front-end (syntax analysis) runs.


Run only the macro preprocessor.


Load the compiler phase specified by c from the specified path.


Pass the specified list of options to the compiler phase specified by c.

Specifying the Buffer Size for Direct Unformatted I/O

You can use the FORTRAN_BUFFER_SIZE environment variable to change the buffer size for direct unformatted I/O. After it is set to 128K (4-byte) words or greater, the I/O on direct unformatted file does not go though the system buffer. No upper limit exists on the number to which you can set FORTRAN_BUFFER_SIZE. However, when it exceeds the system maximum I/O limit, then the Fortran I/O library automatically resets it to the system limit.

See the pe_environ(5) man page for more information.

Object File Tools

The following tools provide information on object files:


Lists headers, tables, and other selected parts of an ELF-format object or archive file.


Disassembles object files into machine instructions.


Prints symbol table information for object and archive files.


Lists the properties of program source, text, object, and other files. This tool often erroneously recognizes command files as C programs. It does not recognize Pascal or LISP programs.


Prints information about the text, rdata, data, sdata, bss, and sbss sections of the specified object or archive files. See the a.out(4) man page for a description of the contents and format of section data.


Removes symbol table and relocation bits.

For more information about these tools, see the MIPSpro Compiling and Performance Tuning Guide and the dis(1), elfdump(1), file(1), nm(1), size(1), and strip(1) man pages.


An archive library is a file that contains one or more routines in object (.o) file format. The term object refers to a .o file that is part of an archive library file. When a program calls an object not explicitly included in the program, the link editor, ld, looks for that object in an archive library. The link editor then loads only that object (not the whole library) and links it with the calling program.

The archiver (ar) creates and maintains archive libraries and has the following main functions:

  • copying new objects into the library

  • replacing existing objects in the library

  • moving objects about the library

  • copying individual objects from the library into individual object files

See the MIPSpro Compiling and Performance Tuning Guide, and the ar(1) man page for additional information about the archiver.

Run-Time Considerations

There are several aspects of compiling that you should consider at run-time. This section discusses some of those aspects:

Invoking a Program

To run a Fortran program, invoke the executable object module produced by the f77 command by entering the name of the module as a command. By default, the name of the executable module is a.out. If you included the -o filename option on the ld (or the f77) command line, the executable object module has the name that you specified.

Maximum Memory Allocations

The total memory allocation for a program, and individual arrays, can exceed 2 gigabytes (2 GB, or 2,048 MB).

Previous implementations of FORTRAN 77 limited the total program size, as well as the size of any single array, to 2 GB. The current release allows the total memory in use by the program to exceed this. For details about the memory use of individual scalar values, see “Alignment, Size, and Value Ranges” in Chapter 2.

Arrays Larger Than 2 Gigabytes

The compiler supports arrays that are larger than 2 gigabytes for programs compiled under the -64 ABI option. The arrays can be local, global, and dynamically created as the following example demonstrates. Initializers are not provided for the arrays in these examples. Large array support is limited to FORTRAN 77, C, and C++.

$cat a2.c

#include <stdlib.h>

int i[0x100000008];

void foo()
int k[0x100000008];
 k[0x100000007] = 9;
 printf(“%d \n”, k[0x100000007]);

char *j;
j = malloc(0x100000008);
 i[0x100000007] = 7;
 j[0x100000007] = 8;
 printf(“%d \n”, i[0x100000007]);
 printf(“%d \n”, j[0x100000007]);

You must run this program on a 64-bit operating system with IRIX version 6.2 or a higher version. You can verify the system type by using the uname -a command. You must have enough swap space to support the working set size and you must have your shell limit datasize, stacksize, and vmemoryuse variables set to values large enough to support the sizes of the arrays. See the sh(1) man page for details.

The following example compiles and runs the above code after setting the stacksize to a correct value:

$uname -a
IRIX64 cydrome 6.2 03131016 IP19
$cc -64 -mips3 a2.c
cputime         unlimited
filesize        unlimited
datasize        unlimited
stacksize       65536 kbytesn
coredumpsize    unlimited
descriptors     200
vmemoryuse      unlimited
$limit stacksize unlimited
cputime         unlimited
filesize        unlimited
datasize        unlimited
stacksize       unlimited
coredumpsize    unlimited
memoryuse       754544 kbytes
descriptors     200
vmemoryuse      unlimited

Local Variable (Stack Frame) Sizes

Arrays that are allocated on the process stack must not exceed 2 GB, but the total of all stack variables can exceed that limit, as in this example:

    parameter (ndim = 16380)
    integer*8 xmat(ndim,ndim), ymat(ndim,ndim), &
    integer k(1073741824)
    integer l(33554432, 256)

However, when an array is passed as an argument, it is not limited in size.

    subroutine abc(k)
    integer k(8589934592_8)

Static and Common Sizes

When compiling with the -static option, global data is allocated as part of the compiled object (.o) file. The total size of any .o file may not exceed 2 GB. However, the total size of a program linked from multiple .o files may exceed 2 GB.

An individual common block may not exceed 2 GB. However, you can declare multiple common blocks, each having that size.

Pointer-based Memory

There is no limit on the size of a pointer-based array, as in this example:

    integer *8 ndim
    parameter (ndim = 20001)
    pointer (xptr, xmat), (yptr, ymat), (zptr, zmat), &
        (aptr, amat)
    xptr = malloc(ndim*ndim*8)
    yptr = malloc(ndim*ndim*8)
    zptr = malloc(ndim*ndim*8)
    aptr = malloc(ndim*ndim*8)

Be sure that malloc is called with an INTEGER*8 value. A count greater than 2 GB would be truncated if assigned to an INTEGER*4.

File Formats

The Fortran compiler supports five kinds of external files:

  • sequential formatted

  • sequential unformatted

  • direct formatted

  • direct unformatted

  • key indexed file

The operating system implements other files as ordinary files and makes no assumptions about their internal structure.

Fortran I/O is based on records. When a program opens a direct file or a key-indexed file, the length of the records must be given. The Fortran I/O system uses the length to make the file appear to be composed of records of the given length. When the record length of a direct unformatted file is 1 byte, the system treats the file as ordinary system files (that is, as byte strings, in which each byte is addressable). A READ or WRITE request on such files consumes bytes until they are used, rather than restricting the request to a single record.

Because of special requirements, sequential unformatted files are usually read or written only by Fortran I/O statements. Each record is preceded and followed by an integer containing the length of the record in bytes.

During a READ, Fortran I/O breaks sequential formatted files into records by using each new line indicator as a record separator. The FORTRAN 77 standard does not define the required result after reading past the end of a record; the I/O system treats the record as being extended by blanks. On output, the I/O system writes a new line indicator at the end of each record. If a user's program also writes a new line indicator, the I/O system treats it as a separate record.

Preconnected Files

The following table shows the standard preconnected files at program start.

Preconnected Files


Alternate Unit

5 (standard input) 

(in READ)

6 (standard output) 

(in WRITE)

0 (standard error) 

(in WRITE)

All other units are also preconnected when execution begins. Unit n is connected to a file named fort.n. These files need not exist, nor will they be created unless their units are used without first executing an open statement. The default connection is for sequentially formatted I/O.

File Positions

The FORTRAN 77 standard does not specify where OPEN should initially position a file that is explicitly opened for sequential I/O. The I/O system positions the file to start of file for both input and output. The execution of an OPEN statement followed by a WRITE on an existing file causes the file to be overwritten, erasing any data in the file. In a program called from a parent process, units 0, 5, and 6 remain where they were positioned by the parent process.

Unknown File Status

When the STATUS="UNKNOWN" parameter is specified in an OPEN statement, the following occurs:

  • If the file does not exist, it is created and positioned at start of file.

  • If the file exists, it is opened and positioned at the beginning of the file.

Quad-Precision Operations

When running programs that contain quad-precision operations, you must run the compiler in round-to-nearest mode. Because this mode is the default, you usually do not have to set it. You will need to set this mode when writing programs that call your own assembly routines. Refer to the swapRM(3c) man page for details.

Run-Time Error Handling

When the Fortran run-time system detects an error, the following actions occur:

  • A message describing the error is written to the standard error unit (unit 0).

  • A core file is produced if the f77_dump_flag environment variable is set. You can use dbx to inspect this file and determine the state of the program at termination. For more information, see the dbx User's Guide.

    To invoke dbx using the core file, enter the following command:

    % dbx binary-file core   

    where binary-file is the name of the object file output (the default is a.out).

Floating Point Exceptions

The libfpe library provides two methods for handling floating point exceptions.

The library provides the handle_sigfpes subroutine and the TRAP_FPE environment variable. Both methods provide mechanisms for handling and classifying floating point exceptions, and for substituting new values. They also provide mechanisms to count, trace, exit, or abort on enabled exceptions. The -TENV:check_div compile option inserts checks for divide by zero or overflow. See the handle_sigfpes(3f) man page for more information.