Example - C Calling Fortran. STMicroelectronics PGI
Pubblicità
Pubblicità
Examples
Compile and execute the program f2c_main.f
with the call to f2c_func_
using the following command lines:
$ pgcc -c f2c_func.c
$ pgf95 f2c_func.o f2c_main.f
Executing the a.out
file should produce the following output:
T v 11 -44 39.6 39.2 981
Example - C Calling Fortran
Example 12.5, “C Main Program c2f_main.c”
shows a C main program that calls the Fortran subroutine
shown in Example 12.6, “Fortran Subroutine c2f_sub.f”
. Notice that each call uses the & operator to pass by reference. Also notice that the call to the Fortran subroutine uses all lower-case and a trailing "_".
Example 12.5. C Main Program c2f_main.c
void main () {
char bool1, letter1;
int numint1, numint2;
float numfloat1;
double numdoub1;
short numshor1;
extern void c2f_func_();
c2f_sub_(&bool1,&letter1,&numint1,&numint2,&numfloat1,&numdoub1,&numshor1, 1);
printf(" %s %c %d %d %3.1f %.0f %d\n",
bool1?"TRUE":"FALSE", letter1, numint1, numint2,
numfloat1, numdoub1, numshor1);
}
158
Example 12.6. Fortran Subroutine c2f_sub.f
subroutine c2f_func ( bool1, letter1, numint1, numint2,
+ numfloat1, numdoub1, numshor1)
logical*1 bool1
character letter1
integer numint1, numint2
double precision numdoub1
real numfloat1
integer*2 numshor1
bool1 = .true.
letter1 = "v"
numint1 = 11
numint2 = -44
numdoub1 = 902
numfloat1 = 39.6
numshor1 = 299
return end
To compile this Fortran subroutine and C program, use the following commands:
$ pgcc -c c2f_main.c
$ pgf95 -Mnomain c2f_main.o c2_sub.f
Scarica
Pubblicità