----------------------------------------------------------------------
Ten dokument jest lekko zmodyfikowana (fragmenty dotyczace SGI) kopia:
http://wwwcn.cern.ch/asdcgi/listcernlibfaqs.pl/10
warto tez spojrzec tu   
                                              marcin
----------------------------------------------------------------------

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
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 cfilename.c
      fort77 +ppu -c ffilename.f
      fort77 +ppu cfilename.o ffilename.o -L /cern/new/lib -lpacklib l .... -o outfilename
      
    2. AIX:
      xlc -c -Dextname -I/cern/pro/include cfilename.c
      xlf -qextname -c ffilename.f
      xlf -qextname cfilename.o ffilename.o -L /cern/new/lib -lpacklib l .... -o outfilename
      
    3. VMS:
      cc /INCLUDE=CERN_ROOT:[INCLUDE] cfilename.c
      fortran ffilename.FOR
      link/exe=outfilename cfilename,ffilename, ...
      
    4. SGI:
      cc -c -Dextname -I/cern/pro/include cfilename.c
      f77 -c ffilename.f
      f77 cfilename.o ffilename.o -L /cern/pro/lib -lpacklib l .... -o outfilename
      

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 chbook-example.c
    fort77 +ppu chbook-example.o `cernlib packlib,mathlib` -o chbook-example
    
    chbook-example
    
  2. AIX:
    xlc -c -Dextname -I/cern/pro/include chbook-example.c
    xlf -qextname chbook-example.o `cernlib packlib,mathlib` -o chbook-example
    
    chbook-example
    
  3. 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
    
  4. SGI:
    cc -c -Dextname -I/cern/pro/include chbook-example.c
    f77 chbook-example.o `cernlib packlib,mathlib` -o chbook-example
    
    chbook-example
    


cernlib@cern.ch