|
But the simplest way is surely to use the cfortran.h package. The complete documentation is in cfortran.doc in ASCII version, but it is strongly recommended to read the HTML version which is much easier to read.
/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:
#include "cfortran.h" #include "hbook.h" /* if you use hbook routines e.g. */
main(){
real xmin,xmax;
integer ncha;
HLIMIT(1000);
.
.
HBOOK1(100,"test spectrum",ncha,xmin,xmax,0);
.
.
}
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
gcc -c -Df2cFortran -I/cern/pro/include/cfortran cfilename.c g77 -c ffilename.f g77 cfilename.o ffilename.o `cernlib packlib ...` ... -o outfilename
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
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
cc -c -I/cern/pro/include/cfortran cfilename.c f77 -c ffilename.f f77 cfilename.o ffilename.o `cernlib packlib ...` ... -o outfilename
cc /INCLUDE=CERN_ROOT:[INCLUDE] cfilename.c fortran ffilename.FOR link/exe=outfilename cfilename,ffilename, ...
g++ -c cfilename.cpp -Df2cFortran -I/cern/pro/include/cfortran g++ -o outfilename cfilename.o `cernlib packlib,mathlib` -lg2c -lm
cxx -c cfilename.cpp -std strict_ansi -msg_disable dollarusedid -Dextname -I/cern/pro/include/cfortran cxx -o outfilename cfilename.o `cernlib packlib,mathlib` -lfor -lm
aCC -c cfilename.cpp -Aa +DAportable -Dextname -I/cern/pro/include/cfortran aCC -o outfilename cfilename.o `cernlib packlib,mathlib` -lm
xlC -c cfilename.cpp -Dextname -I/cern/pro/include/cfortran xlC -o outfilename cfilename.o `cernlib packlib,mathlib` -lxlf90 -lxlf -lm
CC -c cfilename.cpp -I/cern/pro/include/cfortran CC -o outfilename cfilename.o `cernlib packlib,mathlib` -lM77 -lF77 -lsunmath -lm
They have been compiled, linked and run with the following commands (using the first code example)
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
gcc -c -Df2cFortran -I/cern/pro/include/cfortran chbook-example.c g77 chbook-example.o `cernlib packlib,mathlib` -o chbook-example ./chbook-example
xlc -c -Dextname -I/cern/pro/include/cfortran chbook-example.c xlf -qextname chbook-example.o `cernlib packlib,mathlib` -o chbook-example ./chbook-example
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
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:
-lcl -lisamstub Example: gcc chbook-example.o `cernlib packlib,mathlib` -lcl -lisamstub -o chbook-example
in Additional include directories put C:\cern
In Ignore libraries put libcd.lib.
In Additional library path put C:\cern.
/*
* This example demonstrate how to call MINUIT from C.
*
* For question/problems contact: Heplib.Support@cern.ch
*
*
* Authors: William Hanlon <whanlon@cosmic.utah.edu>,
* Gunter Folger <Gunter.Folger@cern.ch>
*/
#include "cfortran.h"
#include "minuit.h"
#define Ncont 20
int main()
{
int error_flag=0;
PROTOCCALLSFSUB0(FCN,fcn)
struct {
double x[Ncont];
double y[Ncont];
int n;
} pts;
double f_null=0.;
MNINIT(5,6,7); /* initialise */
MNSETI(" Minuit Example "); /* set title */
MNPARM(1,"X",0.,.1,f_null,f_null,error_flag);
MNPARM(2,"-Y-",0.,.01,f_null,f_null,error_flag);
MNEXCM(C_FUNCTION(FCN,fcn),"MIGRAD",0,0,error_flag,0);
MNEXCM(C_FUNCTION(FCN,fcn),"MINOS",0,0,error_flag,0);
MNCONT(C_FUNCTION(FCN,fcn),1,2,Ncont,pts.x[0],pts.y[0],pts.n, 0);
}
#include <math.h>
#include <string.h>
#include <cfortran.h>
/* Authors: William Hanlon <whanlon@cosmic.utah.edu>,
* Gunter Folger <Gunter.Folger@cern.ch>
*/
/* prototype for benefit of the wrapper */
void fcn(int npar, double grad[3], double * fcnval,
double xval[2],int iflag, void (*Dummy)());
/* this macro creates a wrapper function called fcn_, which in turn calls the
* function fcn defined below. The wrapper properly passes ints by value to
* fcn, while it receives ints by reference from the fortran calling routine.
*/
FCALLSCSUB6(fcn,FCN,fcn,INT,DOUBLEV,PDOUBLE,DOUBLEV,INT,ROUTINE)
void fcn(int npar, double grad[3], double * fcnval,
double xval[2],int iflag, void (*Dummy)())
{
double Xc=1.11,Yc=3.14;
switch(iflag) {
case 1:
/*
* Initialise.
*/
printf(" fcn_c called to initialise\n");
break;
case 2:
/*
* derivatives...
*/
break;
default:
*fcnval = pow(xval[0]-Xc,2.) + pow((xval[1]-Yc),3.)*xval[1];
break;
}
}