How can I call CERNLIB routines from C?

The following text was adopted from Michael Dahlinger's page at GSI.

General

There are several methods of how to call FORTRAN (most CERN library functions are written in FORTRAN) from a C main program or subroutine. A very good and comprehensive overview of general methods can be found in the article Interfacing Fortran and C (PS) by A.Nathaniel in the CERN Computing Newsletter 217 (PS).

Furthermore, a tool exists (cfortran) to use headerfiles in the C code with the definition of the FORTRAN routines. Then the call of the FORTRAN routine is very easy and all parameters can be handled without special tricks. Especially the calling sequence is independent from the compiler and operating system! Additionally, a tool exists to create such header files (f2h), which is available in the standard CERN program directory. The complete documentation is in cfortran.doc.

Interface to CERN Library routines

To interface the CERN library FORTRAN Routines from C, ready-made header files, created by f2h and exploiting cfortran.h are available. They are stored in

/cern/pro/include/cfortran
or are available using anonymous ftp from

asisftp.cern.ch:/cernlib/share/pro/include

To call a (FORTRAN) CERN library routine from C, you have to do the following steps:

  1. Include in you C files the following header files:
    #include "cfortran.h"
    #include "hbook.h"   /* if you use hbook routines e.g. */
    
  2. Call the fortran routine with "FORTRAN" style parameters (call by value), routine name in capitals:
    main(){
           real xmin,xmax;
           integer ncha;
           HLIMIT(1000);
           .
           .
           HBOOK1(100,"test spectrum",ncha,xmin,xmax,0);
           .
           .
    }  
    
  3. Compile using the following flags:
    1. HP:
      cc -c -Dextname -Ae -I/cern/pro/include/cfortran cfilename.c
      fort77 +ppu -c ffilename.f
      fort77 +ppu cfilename.o ffilename.o `cernlib packlib ...` ... -o outfilename
      
    2. LINUX:
      gcc -c -Df2cFortran -I/cern/pro/include/cfortran cfilename.c
      g77 -c ffilename.f
      g77 cfilename.o ffilename.o `cernlib packlib ...` ... -o outfilename
      
    3. AIX:
      xlc -c -Dextname -I/cern/pro/include/cfortran cfilename.c
      xlf -qextname -c ffilename.f
      xlf -qextname cfilename.o ffilename.o `cernlib packlib ...` ... -o outfilename
      
    4. DIGITAL UNIX/ OSF
      cc -c -Dextname -I/cern/pro/include/cfortran cfilename.c
      f77  -c ffilename.f
      f77 -nofor_main cfilename.o ffilename.o `cernlib packlib ...` ... -o outfilename
      
    5. SUN/ Solaris
      cc -c -I/cern/pro/include/cfortran cfilename.c
      f77  -c ffilename.f
      f77 cfilename.o ffilename.o `cernlib packlib ...` ... -o outfilename
      
    6. VMS:
      cc /INCLUDE=CERN_ROOT:[INCLUDE] cfilename.c
      fortran ffilename.FOR
      link/exe=outfilename cfilename,ffilename, ...
      

Example

Examples show how to use this interface for hbook or minuit:

They have been compiled, linked and run with the following commands (using the first code example)

  1. HP:
    cc -c -Dextname -Ae -I/cern/pro/include/cfortran chbook-example.c
    fort77 +ppu chbook-example.o `cernlib packlib,mathlib` -o chbook-example
    ./chbook-example
    
    
  2. LINUX:
    gcc -c -Df2cFortran -I/cern/pro/include/cfortran chbook-example.c
    g77 chbook-example.o `cernlib packlib,mathlib` -o chbook-example
    ./chbook-example
    
    
  3. AIX:
    xlc -c -Dextname -I/cern/pro/include/cfortran chbook-example.c
    xlf -qextname chbook-example.o `cernlib packlib,mathlib` -o chbook-example
    ./chbook-example
    
    
  4. DIGITAL UNIX /(former) OSF:
    cc -c -Dextname -I/cern/pro/include/cfortran chbook-example.c
    f77 -nofor_main chbook-example.o `cernlib packlib,mathlib` -o chbook-example
    ./chbook-example
    
    
  5. VMS:
    cc /INCLUDE=CERN_ROOT:[INC] chbook-example.c
    link/exe=CHBOOK-EXAMPLE CHBOOK-EXAMPLE.obj,cern_root:[lib]packlib/lib,mathlib/lib,kernlib/lib
    RUN CHBOOK-EXAMPLE
    
    

If you prefer to use the gnu C compiler gcc, you could link using gcc and adding the FORTRAN specific libraries after the CERN libraries. The names of these libraries are different on all systems:

  1. HP:
    -lcl -lisamstub
    Example: gcc chbook-example.o `cernlib packlib,mathlib` -lcl -lisamstub -o chbook-example
    


cernlib@cern.ch