0

我有一个程序,主要由 Fortran 77 和一个 C++ 包装器组成,该包装器可以读取和写入数据库。应用程序的两个部分通过使用一个功能共享数据,如果您使用一个名为 Fortran 公共块的全局 C/C++ 结构,则该功能可以共享数据。我很确定这种 C++/Fortran 集成的别名方法 1) 适用于许多编译器套件,2) 是 *not8 标准。我尝试维护我的代码,以便它尽可能多地使用标准组件。此外,这种集成已被证明是脆弱的。

在 utd.h 中:

/*************************************************************
 *  This struct must follow the common block points.
 *  See the Fortran include file points.i
 *************************************************************/
typedef struct ALIGN points
{
    double  point[3][MAX_PTS];
    double  edge[3][MAX_PTS];
    double  edgenorm[3][MAX_PTS];
    double  edgerho[MAX_PTS];
    int     nfacets[MAX_PTS];
    double  facet1[3][MAX_PTS];
    double  facet2[3][MAX_PTS];
    double  gaussk[MAX_PTS];
    int     sbstin[MAX_PTS];
    int     coatin[MAX_PTS];
    int     sbstout[MAX_PTS];
    int     coatout[MAX_PTS];
    int     ncorners[MAX_PTS];
    double  cnrpoint[3][MAX_CNRS][MAX_PTS];
    int     ncnredgs[MAX_CNRS][MAX_PTS];
    double  cnredge[3][MAX_CNREDS][MAX_CNRS][MAX_PTS];
    int     cnrsbst[MAX_CNREDS][MAX_CNRS][MAX_PTS];
    int     cnrcoat[MAX_CNREDS][MAX_CNRS][MAX_PTS];
    int     npoints;
} POINTS;

extern POINTS  points_;

在 utd.cpp 中:

POINTS  points_;

在points.i中:

  !  maxpnt  -  maximum number of points in a path.
  integer  maxpnt
  parameter ( maxpnt = 1000 )

  integer npoints, nfacets(maxpnt)
  integer ncorners(maxpnt), ncnredgs(maxpnt,maxcorners)
  integer sbstin(maxpnt), coatin(maxpnt)
  integer sbstout(maxpnt), coatout(maxpnt)
  double precision point(maxpnt,3)
  double precision edge(maxpnt,3), edgenorm(maxpnt,3)
  double precision edgerho(maxpnt)
  double precision facet1(maxpnt,3), facet2(maxpnt,3)
  double precision gaussk(maxpnt), cnrpoint(maxpnt,maxcorners,3)
  double precision cnredge(maxpnt,maxcorners,maxcnredges,3)
  integer cnrsbst(maxpnt,maxcorners,maxcnredges)
  integer cnrcoat(maxpnt,maxcorners,maxcnredges)

  common /points/ point, edge, edgenorm, edgerho,
 *                nfacets, facet1, facet2, gaussk,
 *                sbstin, coatin, sbstout, coatout,
 *                ncorners, cnrpoint, ncnredgs,
 *                cnredge, cnrsbst, cnrcoat,
 *                npoints

有没有更好的办法?我可能想将公共块转换为模块。然后,我确信用全局结构为公共块起别名的业务已经不存在了。您如何从 C++ 构建 Fortran 模块?你如何从这样的模块中读取数据?

您对 C++/Fortran 集成有何建议?

4

1 回答 1

5

The ISO C Binding of Fortran 2003 makes interoperability of C and Fortran part of the Fortran language standard, and therefore compiler and platform portable. If you only use features of C, most likely it will work in C++, though perhaps that is not absolutely guaranteed. You can mix Fortran 2003 and FORTRAN 77, either within a source file or by mixing routines written in the two versions of the languages. The ISO C Binding supports structures and module variables as global variables, so it can accomplish your goals. Both of these are shown in the Mixed Language Programming Chapter of the gfortran manual. (The ISO C Binding is not specific to gfortran, I'm citing that as a good document.)

Common blocks are also supported, but I agree with you, best avoided ... common blocks add on FORTRAN storage sequence and layout, which is unnecessary. I don't see any way around manually maintaining the C and Fortran "structures" to have equivalent declarations. If that is the reason for the brittleness you will just have to be careful (baring writing a program to write both from common instructions). One approach that would make it a little easier would be not to put the variables into a structure ... then getting a new variable wrong is less likely to effect the older variables and that might make noticing or debugging a mistake easier.

于 2012-08-04T19:25:00.320 回答