In principle it is possible to write user code in C or C++ and link it against FORTRAN code. There are a few rules which have to be followed and considered.
1.) Any parameter passed from FORTRAN or to FORTRAN is by reference and not by value! Thus,
one must use pointers in C/C++.
2.) Row and column indices of 2D arrays are inverted between FORTRAN and C. For example
MATRIX(a,b) in FORTRAN must be accessed as
MATRIX[b][a] in C/C++.
3.) Any FORTRAN routine that you call from C/C++ must be declared adhering to the following rules:
a.) names must be lower case only
b.) the function name must be followed by an underscore!!
c.) all variables must be passed by reference
For example to call FLUKA’s function
GEOREG one must add the following declaration on the C/C++ side:
extern "C" void georeg_( double* X, double* Y, double* Z, int* RegNr, int* Disc ); // SUBROUTINE GEOREG ( X, Y, Z, NREG, IDISC )
It can then be called like
double X, Y, Z; int RegNr = -1, Disc = -1; X = 100; Y = 200; Z = 300; // call FLUKA's geometry kernel to determine the region number of the current position georeg_( &X, &Y, &Z, &RegNr, &Disc );
4.) Calling C++ classes/functions from FORTRAN is not directly possible. However, you can call any C function and in those functions you can then use C++ objects etc. The C functions callable from FORTRAN must adhere to the same naming scheme as above. For example:
extern "C" void myparser_( char* pInputFile );
This function can then be called from FORTRAN via:
CHARACTER SDUM*8 CALL MYPARSER(SDUM)
5.) If you need to use the C++ standard library it must be linked passing the option
-astdc++ to the lfluka script before invoking
lfluka -o myexec -astdc++ -m fluka $(CPPOBJS) $(FOBJS)
Otherwise one might only get the error message “Unknown file” from the linker without any other information which file is concerned!