HP Fortran Programmer's Guide (March 2010)
Calling C routines from HP Fortran
Sharing data
Chapter 8 211
10 FORMAT(3X, F3.0, 2X, A, 8X, F5.2)
END PROGRAM main
Example 8-11 shared_struct.c
#include <stdio.h>
#include <math.h>
/* declare a structure whose members match the data items
* in the Fortran common block
*/
struct glob
{
double num;
double nlog_of_num;
} globals;
/* get_nlog: reads the value in globals.num, passes it
* to log() in the math library, and writes the write the
* return value to globals.nlog_of_num
*/
void get_nlog(void)
{
/* declare the name of the common block defined in the
* Fortran file
*/
extern struct glob globals;
globals.nlog_of_num = log(globals.num);
}
Below are the command lines to compile, link, and execute the program, followed by the
output from a sample run. The -lm option at the end of second command line tells the linker
to look in the math library for the log function:
$ cc -Aa -c shared_struct.c
$ f90 shared_common.f90 shared_struct.o -lm
$ a.out
Number Natural Log of Number
-------+-----------------------
2. | 0.69
3. | 1.10
4. | 1.39
5. | 1.61
6. | 1.79
7. | 1.95
8. | 2.08
9. | 2.20
10. | 2.30