[GSI Theory]

Theory Computing Environment

Compilers



News
Linux Cluster
AIX Cluster

writing docu

Software
 
Scripting
Compilers
Batch System
Mathematics
Graphics
Typesetting
Communications
Desktop

Configuration
 
Shells
X11

 

Compiling and Debugging

Thomas Neff



Compilers
Compilers for C (gcc), C++ (g++) and Fortran 77 (g77) are available. They follow the usual UNIX conventions. Compilation of individual source files
gcc -c csource.c 
g++ -c cppsource.cc 
g77 -c fsource.f 
results in the corresponding object files. In the linking stage non-standard libraries have to be specified. To compile and link a C program myprog.c which calls routines from the lapack- and blas- libraries
gcc -c myprog.c
gcc -o myprog -llapack -lblas -lm myprog.o
Compiling and linking can be done in one step
gcc -o myprog -llapack -lblas -lm myprog.c

Compiler Switches

The following switches tell the compiler to be verbose. Warnings don't prevent compilation but they let you spot possible problems with your code. If you wan't to debug your code, compile it with -g.
-Wallswitch on all warnings
-Wunusedtell me about unused variables
-Wimplicitg77 - no implicit declarations allowed
-ginclude debugging information
The following switches are recommended for production. Try with -O and -O2 to see which gives better results on your code, -fno-emulate-complex is useful if you do complex arithmetic in your Fortran code.
-Oturn on optimizations
-O2try harder on optimizations
-malign-doublealign data on stack and heap
-march=pentiumprotune code for Pentium Pro and Pentium II
-fno-emulate-complexg77 - don't emulate complex numbers by two reals
For detailed information see the info pages gcc.info and g77.info

Linking Fortran and C
You can link Fortran and C modules into one executable. If you want to call Fortran routines from C you have to respect Fortrans calling conventions. Fortran symbols get an extra underscore and arguments to functions and subroutines are passed by reference. Fortran strings are a special problem and should be avoided. Have a look a this very simple program consisting of two C modules main.c, cmodule.c and one Fortran module fmodule.f.
main.c
#include <stdio.h>

extern double callc(double x);
extern double callf_(const double* x);

int main()
{
    double x=2.0;
    double cres, fres;

    /* call the C routine */
    cres = callc(x);

    /* call the Fortran routine */
    fres = callf_(&x);

    printf("The C result: %8.2g\n", cres);
    printf("The Fortran result: %8.2g\n", fres);

    exit(0);
}
cmodule.c
double callc(double x)
{
   return x*x;
}
fmodule.f
real*8 callf(x)
real*8 x

callf = x*x*x
return
To compile the modules
gcc -c main.c 
gcc -c cmodule.c 
g77 -c fmodule.f 
To link the modules into one executable proggy use
gcc -o proggy main.o cmodule.o fmodule.o -lg2c -lm 
Here gcc has been called for linking. gcc has been told to also link the Fortran library libg2c and the math library libm.

Debugging
If you want to debug your program compile it with -g. The compiler then puts information about the source code into the executable. Debugging with optimized programs can be difficult if the compiler optimized some variables away. The GNU debugger gdb is a command line debugger. The Data Display Debugger ddd is graphical frontend to gdb. For detailed instructions on debugging see gdb.info.

Post-mortem debugging

If the program proggy dumps core it is possibly to do a post-mortem debugging session. You have to tell the debugger gdb about your executable and the core dump
gdb proggy core
You can then get the stack with the command bt. The debugger also tells you function and line where the program stopped.


Last modified: January 21, 2004 Thomas Neff