Unix/Linux/Cygwin Software Development
Installed Compilers
Compilers available include the the gnu compilers, gcc, g++, g77 on all hosts; Lahey fortran, lf95 on the Linux hosts; the Intel compilers, icc and ifc on the Linux hosts; and Intel fortran on the Windows host ghostwheel . TTU also has a site license for Microsoft C (Visual Studio), and it is installed on the Windows host.
Unix Development Environment Basic Features
The Linux compilers and gnu compilers under cygwin all operate similarly. Here we will refer to the various compilers as cc
for brevity.
The simplest way to compile is:
% cc myprog.c
Since a source file myprog.c is specified, and the compile-only option -c is not specified, the program will try to both compile (create object files) and link the object files into an executable. This may be separated into two steps:
% cc -c myprog.c
% cc myprog.o
Normal Unix/Linux practice is to keep all source files pertaining to one executable in a single directory, and to break up the source files, using csplit or fsplit so that each function or subroutine corresponds to a single file. In that case, every source file may be compiled together, and every object file may be linked together:
% cc -c *.c
% cc *.o
If there is more than one main program, the linker may have problems deciding which is the correct one to use.
Other common features are
include files (source files which contain common data such as declarations) and link libraries (libraries of object files which are linked to multiple programs). Include files are referenced by statements such as
#include "f2c.h"
The compiler looks for include files in a standard location (usually /usr/include and other locations as specified by
-I on the compile step. The convention is to keep include files in a subdirectory include :
% cc -I/usr/local/mpich-1.2.4/include -c *.c
Link libraries are collections of object files which are shared between compiles. The convention is to keep them in a subdirectory
lib and name them libxx.a or libxx.so . The link reference is then -lxx in the link step,
for example libmpich.a . The linker will search in several locations, depending on system, usually beginning with
/usr/lib and ending with the directories specified in -L :
% cc *.o -L/usr/local/mpich-1.2.4/lib -lmpich
Another common features is optimization, specified by capital letter O, as -O,-O1, -O2, -O3 depending on compiler. It is
turned off at level zero: -O0 and should be off in development code. Also common is debugging, specified
by
-g in
development code. It shoud be turned off in production code:
-g0 .
Debugging code can be much slower at runtime. For
development:
% cc -g -O0 *.c
For production:
% cc -g0 -O3 *.c
Another common option is to name the output executable in
the link step using the lower case letter o. Otherwise the
executable will be named
a.out , or
a.exe on cygwin.
% cc -omyexec *.o -L/usr/local/mpich-1.2.4/lib
-lmpich
Installed compilers on the clusters are the GNU compilers
gcc (c), <
Intel Fortran and c compilers. Compiler options vary
substantially betweenGNU, Lahey, and Intel compilers. Each
has dozens of options, a very few will be covered here.
% gcc myprog.c
(clusters gnu c)
% g++ myprog.cc
(clusters gnu c++)
% g77 myprog.f
(clusters gnu f77)
% lf95 myprog.f90
(Lahey f90 or f77)
% ifc myprog.f90
(clusters Intel f90 or f77)
% icc myprog.c
(clusters Intel c)
If the compile and link steps are successful, these will
produce the default executable file
a.out which can
then be run as
% ./a.out
Each compiler defaults to "no optimization" or
-O0 (dash capital
"Oh" zero) so code produced this way will usually run
slowly. Normally you will want to add optimization to the
compile step. The available optimization levels are:
GNU:
-O0 (default), -O1,
-O2, -O3
Lahey:
-O0 (default), -O
Intel:
-O0 (default), -O1
-O2 -O3
Optimization levels are specified for example as:
% cc -O3 myprog.c
% f90 -O2 myprog.f
Each level of optimization takes longer to compile but
should take less time to execute. High levels of
optimization (such as
-O3
and -Ofast ) may slightly change the program
results by reordering code and speeding floating point
calculations, so the program results should be checked
against known results or
-O0 results.
Some compilers produce a debuggable executable by default
(specified by
-g ),
which may be run under debuggers such as
dbx . The
debuggable version is usually very much slower. For
performance specify
-g0 .
You may also compile and link together every source code
file in the current directory:
% cc -O2 *.c
% f90 -O2 *.f
This assumes that one file contains the main program and the
others contain functions or subroutines. Otherwise the
linker will probably get confused.
You may also name the resulting executable file other than
a.out with
-o (dash
lower case "oh"):
% cc -O2 -omyexe *.c
% f90 -O2 -omyexe *.f
Sometimes you may want to compile to object files and link
the object files in separate steps:
% cc -O2 -c *.c
% f90 -O2 -c *.f
which produce object files
*.o , and
% cc -omyexe *.o -lm
% f90 -omyexe *.o -lm
which link object files and the standard math library
libm.a or
libm.so to form
an executable
myexe .
These compile/link steps often specify directories
-I to specify the
location of "include" files such as
#include "myprog.h"
and
-L to
specify the location of link library files or external
functions:
% cc -O2
-I/usr/local/mpich/include -c *.c
% cc
-L/usr/local/mpich/lib -omyexe *.o -lmpich -lm
% f90 -O3 -omyexe
-I/usr/local/mpich/include *.f -L/usr/local/mpich/lib
-lmpich -lm
Here the
-lmpich
indicates a link library
libmpich.so or
libmpich.a in the
directory
/usr/local/mpich/lib . Many standard include
files such as
math.h
are contained in system directories such as
/usr/include , and
many standard link libraries such as and
libstdio.a or
libm.so are
contained in system directories such as
/usr/lib . For
these standard files it is usually not necessary to specify
-I or
-L , but all
required link libraries, in standard directories or not,
must be specified in the link step:
% cc -omyexe *.o -lm
-lmpi`
% f90 -omyexe *.o -lm
-lmpi`
Compiler Options Table
| Software | Usage [options] |
| Compilers | (SGI:) [f77/f90/cc/CC] (GNU:) [gcc/g++] (Lahey:) [lf95] |
| Memory model | [ (SGI memory <2G:) -n32 , (SGI memory >2G:) -64 , SGI old style: -o32 Intel: no options ] |
| Debug options | SGI:
[f90/cc] [-64/-n32]
-trapeuv
-DEBUG:div_check=3:subscript_check=ON:verbose_runtime=ON:\ fullwarn=ON] [ Fortran only: -static] [test.f/test.c] |
| Performance options | SGI:
[f90/cc]
-TARG:platform=ip27 [-64/-n32] -g0 [-O3 /
-Ofast=IP27] -IPA [test.f/test.c] -lfastm GNU: [gcc/g77] -g0 -O3 [test.f/test.c] -lm Lahey: lf95 -g0 -O test.f |
| OpenGL glut c | SGI: cc test17.c -lglut -lGL -lGLU -lXext -lX11 -lXmu -lXi -lm |
| OpenGL glut f77 | SGI:
f90 fbitfont.f -L/usr/local/glut-3.7.1/lib/fglut -lfglut
\ -lfGL -lfGLU -lglut -lGL -lGLU -lXext -lX11 -lXmu -lXi -lm |
| Lapack, Blas | SGI: [f90/cc] [performance options] [test.f/test.c] -lscs |
| Auto. shared-mem mp | SGI: [f90/cc] [-64/-n32] [performance options] -apo [test.f/test.c] -mp |
| MPI | SGI:
[f90/cc] [-64/-n32]
[performance options]
[test.f/test.c] -lmpi Lahey: lf95 -O --staticlink -L/home/local/lib test.f -lfmpich -lmpichf90 -lmpich |
| make |
gmake (executes the script called
Makefile
, updates/recompiles executable from source files). Dependency lines matmul:*.o etc. must begin in column 1. Command lines $(CC) -o matmul etc. must begin with a tab character (shown as ^I). The gnu version gmake is usually better to use as it is more forgiving than the irix version make . Below are basic Makefiles don't include the (comments). |
| c make |
CC=cc CFLAGS=-O matmul:*.o (executable matmul depends on object files *.o ) ^I$(CC) -o matmul *.o -lm (use the loader cc -o to make matmul ) *.o:*.c (object files *.o depend on c files *.c ) ^I$(CC) -c $(CFLAGS) *.c (use the compiler cc -c to make *.o ) |
| f90 make |
F90=f90 FFLAGS=-O3 -IPA LIBS=-lmpi matmul:*.o (executable matmul depends on object files *.o ) ^I$(F90) -o matmul *.o (LIBS) (use the loader f90 -o to make matmul ) *.o:*.f (object files *.o depend on fortran files *.c ) ^I$(F90) -c $(FFLAGS) *.f (use the compiler f90 -c to make *.o ) |
Detailed tutorial from SGI at U.Kansas
Origin tips at Ga. Tech