I am trying to define and declare a set of structures in C, then interact with them in fortran before passing them back off to be manipulated in C functions. I understand that interoperability can be achieved through use of c structures and fortran common blocks. I have tried a few methods with no success.
Limitations: Using cvfortran, f77. It is not realistic to convert to newer formats. For C, using MSVS C compiler.
Here's the suggestion from the cvfortran manual, page 618: (http://jp.xlsoft.com/documents/intel/cvf/cvf_pg.pdf)
"As an example, suppose your Fortran code has a common block named Really, as shown:"
!DEC$ ATTRIBUTES ALIAS:'Really' :: Really
REAL(4) x, y, z(6)
REAL(8) ydbl
COMMON / Really / x, y, z(6), ydbl
"You can access this data structure from your C code with the following external data structure:"
#pragma pack(2)
extern struct {
float x, y, z[6];
double ydbl;
} Really;
#pragma pack()
Whenever I try this, I get an error for "Unresolved external symbol _Really refereced in function xxx, and it doesn't compile.
Method in CVFortran Manual:
Fortran
PROGRAM MYPROG
!DEC$ ATTRIBUTES ALIAS:'Really' :: Really
REAL(4) x, y, z
COMMON / Really / x, y, z
INTERFACE
SUBROUTINE STRUCTFUN()
cDEC$ ATTRIBUTES C, ALIAS:'_StructFun' :: STRUCTFUN ! for calling C functions
END SUBROUTINE STRUCTFUN
END INTERFACE
X = 6.
Y = 5.
Z = 0.
CALL STRUCTFUN()
END PROGRAM
C
#include <stdio.h>
void StructFun(void)
{
#pragma pack(2)
extern struct {
float x, y, z;
} Really;
#pragma pack()
printf("x: %f\n y: %f\n z: %f\n", Really.x, Really.y, Really.z);
printf("From C \n");
}
So then I tried what I have pasted below, in which I define a struct type in the header file, and then try to make it external to c. I should also mention that I am exporting the C function as a dll with a .def file so that it can be called from my fortran module. This gives no errors but returns values of zero for all of my variables.
Sample program:
Fortran
PROGRAM MYPROG
REAL(4) X,Y,Z
COMMON / REALLY / X, Y, Z
INTERFACE
SUBROUTINE STRUCTFUN()
cDEC$ ATTRIBUTES C, ALIAS:'_StructFun' :: STRUCTFUN ! for calling C functions
END SUBROUTINE STRUCTFUN
END INTERFACE
X = 6.
Y = 5.
Z = 0.
CALL STRUCTFUN()
END PROGRAM
C function
#include "structProg.h"
rtype really_;
void StructFun(void)
{
printf("x: %f\n y: %f\n z: %f\n", really_.x, really_.y, really_.z);
printf("From C \n");
}
C header file
#include <string.h>
#include <stdio.h>
typedef struct{
float x;
float y;
float z;
} rtype;
extern rtype really_;
extern void StructFun(void);
I know it's a stretch to ask this since I'm using an old compiler, but any guidance would be appreciated.
I've also tried a method listed here: http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html