0

我有一个 *.H 定义一个结构。像这样:

#define nfloats 9999
#define nword   655

typedef struct {

int a
short b
unsigned short d
float e
char t[nword]
short un[14]
float  dat[nfloats];

} datafile

在 void c 中,数据文件的所有参数都被赋值。我想将分配的值传递给主 fortran 程序,而不必使用 TYPE 重新定义。只需调用 *.H 和 iso_c_binding。换句话说,我希望在 fortran 主程序中使用已经在 *. H. 有人提出什么建议?

非常感谢!

4

1 回答 1

0

The easiest way is to write a script in your favourite language that reads the C definition and converts it to Fortran. If you fix your syntax then it is quite easy.

1) #define xxx yyy becomes integer, parameter:: xxx = yyy

2) There is no unsigned in Fortran so unsigned shorts and shorts are both integer(kind=1).

3) It depends on how you are using the character arrays. If they are returning individual characters, not assigned en block, then they can be declared as character t(nword). If they are expected to hold strings then they should be declared character(len=nword) t.

4) typedef struct { ... } datafile

becomes

type datafile sequence ... end type

5) Having done all that, you need to make sure that both the C and Fortran code have the same byte alignment. Your char array of 655 is not on a byte boundary so you might get alignment problems.

A second method is to play with macros and get the macros to do the generation for you. You need to define one set of macros for C and another set for Fortran. So you'd get something like

STRUCT_BEG(datafile)
   INTEGER(a)
   SHORT(b)
   ...
   CHARA(t,nword)
   FLOATA(dat,nfloats)
STRUCTEND(datafile)

The C defines would be

#define STRUCT_BEG(ignore) typedef struct {
#define STRUCT_END(name)   } name;
#define INTEGER(a) int a;
#define CHARA(a,length) char a[length];

The Fortran defines would be something like

#define STRUCT_BEG(name) \
    type name \
        sequence
#define STRUCT_END(ignore) end type
#define INTEGER(a) integer::a
#define CHARA(a,length) character(len=length):: a

If you compile with -e, the C compiler can be used to generate both headers.

于 2013-04-13T19:45:52.063 回答