如何从常见块重新设计Fortran和C++混合语言库 - 全局结构别名?

ems*_*msr 0 c++ fortran g++ gfortran fortran-common-block

我有一个程序,主要由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_;
Run Code Online (Sandbox Code Playgroud)

在utd.cpp中:

POINTS  points_;
Run Code Online (Sandbox Code Playgroud)

在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
Run Code Online (Sandbox Code Playgroud)

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

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

M. *_* B. 5

Fortran 2003的ISO C绑定使得Fortran语言标准的C和Fortran部分的互操作性成为可能,因此编译器和平台也是可移植的.如果你只使用C的功能,很可能它将在C++中工作,尽管可能并非绝对保证.您可以在源文件中混合使用Fortran 2003和FORTRAN 77,也可以混合使用两种语言版本编写的例程.ISO C Binding支持结构和模块变量作为全局变量,因此它可以实现您的目标.这两个都显示在gfortran手册的混合语言编程章节中.(ISO C绑定并不是特定于gfortran,我引用它作为一个好文档.)

也支持公共块,但我同意你的意见,最好避免...常见块添加FORTRAN存储序列和布局,这是不必要的.我没有看到任何方法手动维护C和Fortran"结构"具有相同的声明.如果这是脆弱的原因,你只需要小心(禁止编写一个程序来编写通用指令).一种让它变得更容易的方法就是不将变量放入结构中......然后将新变量设置为错误不太可能影响旧变量,这可能会更容易注意到或调试错误.