Tutorial: Using C/C++ and Fortran together
Tutorial: Using C/C++ and Fortran together
Comparison of FORTRAN and C/C++ datatypes:
FORTRAN C/C++
integer*2 short int
integer long int or int
integer iabc(2,3) int iabc[3][2];
logical long int or int
logical*1 bool
(C++, One byte)
real float
real*8 double
complex struct{float r, i;}
double complex struct{double dr, di;}
character*6 abc char abc[6];
parameter #define PARAMETER value
Note:
* Order of multi dimensional arrays in C/C++ is the opposite of FORTRAN.
* It is best not to re-dimension multi dimensional arrays within a function. Pass array size "n" and declare array as x[n][];
Linking FORTRAN And C Subroutines:
Note: The entry point names for some FORTRAN compilers have an underscore appended to the name. This is also true for common block/structure names as shown above.
FORTRAN C
call subrA( ... ) subra_( ... )
The f77 comiler flags "-fno-underscore" and "-fno-second-underscore" will alter the default naming in the object code and thus affect linking. One may view the object file with the command nm (i.e.: nm file.o).
Note: The case in FORTRAN is NOT preserved and is represented in lower case in the object file. The g77 compiler option "-fsource-case-lower" is default. GNU g77 FORTRAN can be case sensitive with the compile option "-fsource-case-preserve".
NOTE: When debugging with GDB, the Fortran subroutines must be referenced with names as they appear in the symbol table. This is the same as the "C" representation. Thus when setting a break point at the Fortran subroutine subrA(), issue the comand "break subra_".
Man pages:
* nm - list symbols from object files
* g77/f77
=============================================
C++ calling a FORTRAN function:
testC.cpp
#include
using namespace std;
extern"C" {
void fortfunc_(int *ii, float *ff);
}
main()
{
int ii=5;
float ff=5.5;
fortfunc_(&ii, &ff);
return 0;
}
testF.f
subroutine fortfunc(ii,ff)
integer ii
real*4 ff
write(6,100) ii, ff
100 format('ii=',i2,' ff=',f6.3)
return
end
Compile:
* f77 -c testF.f
* g++ -c testC.cpp
* g++ -o test testF.o testC.o -lg2c
Run: ./test
ii= 5 ff= 5.500
Comparison of FORTRAN and C/C++ datatypes:
FORTRAN C/C++
integer*2 short int
integer long int or int
integer iabc(2,3) int iabc[3][2];
logical long int or int
logical*1 bool
(C++, One byte)
real float
real*8 double
complex struct{float r, i;}
double complex struct{double dr, di;}
character*6 abc char abc[6];
parameter #define PARAMETER value
Note:
* Order of multi dimensional arrays in C/C++ is the opposite of FORTRAN.
* It is best not to re-dimension multi dimensional arrays within a function. Pass array size "n" and declare array as x[n][];
Linking FORTRAN And C Subroutines:
Note: The entry point names for some FORTRAN compilers have an underscore appended to the name. This is also true for common block/structure names as shown above.
FORTRAN C
call subrA( ... ) subra_( ... )
The f77 comiler flags "-fno-underscore" and "-fno-second-underscore" will alter the default naming in the object code and thus affect linking. One may view the object file with the command nm (i.e.: nm file.o).
Note: The case in FORTRAN is NOT preserved and is represented in lower case in the object file. The g77 compiler option "-fsource-case-lower" is default. GNU g77 FORTRAN can be case sensitive with the compile option "-fsource-case-preserve".
NOTE: When debugging with GDB, the Fortran subroutines must be referenced with names as they appear in the symbol table. This is the same as the "C" representation. Thus when setting a break point at the Fortran subroutine subrA(), issue the comand "break subra_".
Man pages:
* nm - list symbols from object files
* g77/f77
=============================================
C++ calling a FORTRAN function:
testC.cpp
#include
using namespace std;
extern"C" {
void fortfunc_(int *ii, float *ff);
}
main()
{
int ii=5;
float ff=5.5;
fortfunc_(&ii, &ff);
return 0;
}
testF.f
subroutine fortfunc(ii,ff)
integer ii
real*4 ff
write(6,100) ii, ff
100 format('ii=',i2,' ff=',f6.3)
return
end
Compile:
* f77 -c testF.f
* g++ -c testC.cpp
* g++ -o test testF.o testC.o -lg2c
Run: ./test
ii= 5 ff= 5.500

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home