gpt4 book ai didi

c++ - 如何从公共(public) block 重新设计 Fortran 和 C++ 混合语言库 - 全局结构别名?

转载 作者:太空宇宙 更新时间:2023-11-04 16:26:13 24 4
gpt4 key购买 nike

我有一个程序,主要由 Fortran 77 和一个 C++ 包装器组成,可以读取和写入数据库。应用程序的两个部分通过使用一个功能共享数据,如果您使用一个名为 Fortran 公共(public) block 的全局 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

有没有更好的方法?我可能想将公共(public) block 转换为模块。然后,我确定这种使用全局结构为公共(public) block 别名的业务已经过时了。您如何从 C++ 构建 Fortran 模块?你如何从这样的模块中读取数据?

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

最佳答案

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

公用 block 也受支持,但我同意你的看法,最好避免...公用 block 添加在 FORTRAN 存储序列和布局上,这是不必要的。我看不出有任何方法可以手动维护 C 和 Fortran“结构”以具有等效声明。如果这是脆弱的原因,你只需要小心(除非编写一个程序来从通用指令编写)。一种使它更容易一点的方法是不将变量放入结构中......然后让一个新变量出错不太可能影响旧变量并且可能更容易注意到或调试错误。

关于c++ - 如何从公共(public) block 重新设计 Fortran 和 C++ 混合语言库 - 全局结构别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11811114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com