Compiling Fortran and C programs



Last revision August 4, 2004

Table of Contents:
  1. cc is the C language compiler and linker
  2. f77 is the Fortran language compiler
  3. ld is the basic loader or linker program
  4. Running your executable programs

ld is the basic loader or linker program

ld expects to be given the names of object code (.o) files as arguments; it links them together into an executable program and resolves references.

Because each programming language has its own set of standard system routines that must be linked in with your compiled routines to make a complete executable program, ld is normally not called directly by users. Instead, you use cc or f77 (or another compiler program), giving object file names as arguments. Those compiler programs call ld for you with options listing the correct system routine libraries. In this case, you can specify additional ld options (as documented in the manual entry) on the command line of the cc, f77, or other compiler program.

Most useful link-time options (use with ld, f77, or cc):

-o outputname
Gives the resulting executable program the name outputname instead of the default name a.out.

-lxxx
(letter "ell" option flag, not number one). xxx is any string of characters immediately following the -l (no blanks). This option is used to search a library of object modules for unresolved references (additional routines not part of your object modules). This option lets you specify a library of object modules to be searched in addition to the standard system libraries which are searched by default. For example, the option naval is used to search the naval library of mathematical routines.

The directories /lib, /usr/lib, and /usr/local/lib are searched, in that order, to find the library.

The actual filename for the library is constructed by prefixing the xxx with lib and suffixing it with .a. For example, the option
     -lnaval
means to search the library with filename
     libnaval.a
More than one -l option may be specified.

The order in which you mix object file names and -l options is important, since libraries are searched for unresolved references only once, in the order encountered on the command line. Thus, put all library references at the end of the command line, and if one library has routines that depend on routines in another library, put the dependent one first.

In the C language, some things that you think of as standard functions, such as trigonometric math functions, are actually implemented in libraries which must be linked into your program. For example, the math functions are linked in by specifying the option
     -lm
to the cc command.

-Ldir
Use this option to add another directory to the list to be searched for either system libraries or user-specified libraries. The -L (uppercase) is followed immediately by the name of a directory (dir). Multiple -L options may be given to specify multiple search directories. They must be specified before the -lxxx options that describe the names of the actual object code libraries.