Fortran 和 C 混合编程

发布于 2024-12-09 12:15:05 字数 432 浏览 0 评论 0原文

我是一名理论物理研究生,从事宇宙学研究。在我的研究过程中,我使用了相当庞大的 Fortran 代码库,并使用 C 来满足我的编程需求。

我已经能够在大量测试文件中链接这两个程序,并且它们运行得非常好。但对于他们来说,我一直使用目标文件来链接它们。但是当我尝试通过 C 运行真正的交易时,包括对 Fortran 头文件的引用。它们似乎可以很好地集成和调用,但 Fortran 头文件的格式与 C 编译器不兼容,因此当它跳转到头文件时,它开始抛出无法理解语法的错误。

例如,Fortran 头文件使用 real*8 定义双精度变量,因此当 C 读取它们时会抛出错误。文件中的注释也会发生同样的情况。

那么我想请问有什么办法可以解决这个问题吗?即使 fortran 格式的头文件可以通过 C 读取。

我浏览了互联网并发现了令人困惑的答案,我不知道该遵循哪一个。任何有关此事的帮助将不胜感激:)

I am a Theoretical Physics research student, working in Cosmology. In course of my research I have use to rather huge library of Fortran codes and I used C for my programming needs.

I have been able to link the two programs in numerous test files and they work brilliantly. But for them I have been using the object files to link them all. But when I tried to run the real deal through C, include reference to the Fortran header files. They seem to integrate and call each other fine but the format of the Fortran header file is incomptible with the C compiler, so when it jumps to the header file it start throwing errors that it cannot understand the syntax.

For example, the Fortran header file defines double variables with real*8 so when C reads them it throws errors. The same happens with comments in the file as well.

So, I want to ask is there any way through which I can go about this problem? i.e. make the fortran format header file readible through C.

I looked over the internet and found confusing answers, and I do not know which one to follow. Any help in this matter will be appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不在是我 2024-12-16 12:15:05

抱歉,你太令人困惑了。什么是 Fortran 头文件?例如,您无法使用 C 编译器读取 Fortran 包含文件!两种语言差别太大了。此外,Fortran 包含文件几乎从来都不是与 C 的头文件可比的头文件。

不知道你用的是什么编译器。但如果您选择了最新的 GCC 版本(Gnu 编译器集合),那么其中包含的 Fortran 编译器能够考虑 ISO_C_BINDING 功能,这使得 Fortran-C 的耦合更加容易。

示例:

MODULE my_fortran
  USE iso_c_binding
  IMPLICIT NONE
  CONTAINS
  SUBROUTINE my_subroutine(a,b) BIND(C,name="my_sub")
    INTEGER(c_int),INTENT(in),VALUE :: a
    REAL(C_DOUBLE),INTENT(out) :: b
    ...
  END SUBROUTINE
END MODULE

名为“my_sub.h”的 C 头文件 例如:

void my_sub(int, double *);

C 文件

#include "my_sub.h"

int main(){
  double b;
  int a=3;
  my_sub(a,&b);
  ...
}

Sorry but you are very confusing. What is a Fortran header file ? For instance, you cannot read a Fortran include file using a C compiler ! The two languages are too different. In addition a Fortran include file is almost never an header file comparable to the C's one.

I don't know the kind of compiler you are using. But if you have chosen a recent GCC version (Gnu Compiler Collection), then the Fortran compiler included inside is able to take into account the ISO_C_BINDING feature which makes easier the coupling Fortran-C.

Example :

MODULE my_fortran
  USE iso_c_binding
  IMPLICIT NONE
  CONTAINS
  SUBROUTINE my_subroutine(a,b) BIND(C,name="my_sub")
    INTEGER(c_int),INTENT(in),VALUE :: a
    REAL(C_DOUBLE),INTENT(out) :: b
    ...
  END SUBROUTINE
END MODULE

C header file named "my_sub.h" for instance :

void my_sub(int, double *);

C file

#include "my_sub.h"

int main(){
  double b;
  int a=3;
  my_sub(a,&b);
  ...
}
尘世孤行 2024-12-16 12:15:05

Fortran 通常通过引用传递变量(传递指针)。
这意味着您必须在调用的 C 程序中给出地址。

函数结果,可以按值传递,例如以下
代码调用一个名为“gamma”的 FORTRAN 函数:

double   x, y;
   ..................
   x = 2.0; 
   y = gamma_(&x) 

确保
调用程序中变量的大小与大小相同
在 Fortran 例程中:

float  --- REAL     (this is typical, but not always the case)
double --- REAL*8

Fortran 函数必须在 C 调用函数的开头声明:

extern void read_(int *small, float *medium, double *large);

注意我们必须将所有变量作为指针传递给 Fortran。尽管 Fortran 中的函数名称不区分大小写,但它在 C 声明和调用时会获得一个下划线:

 read_(&small, &medium, &large);

Fortran 函数按如下方式接收变量:

SUBROUTINE READ(small,medium,large)

  INTEGER       small
  REAL          medium
  DOUBLE        large

这些变量的精确大小取决于系统的体系结构(32 位与 64 位)位),因此您需要确认您系统上的 C 和 Fortran 中 int、float 和 double 之间的对应关系。

Fortran usually passes its variables by reference (passes pointers).
That means that you MUST give addresses in your calling C-program.

Function results, may be passed by value, for example, the following
code calls a FORTRAN function called "gamma":

double   x, y;
   ..................
   x = 2.0; 
   y = gamma_(&x) 

Make sure that the
size of the variable in the calling program is identical to the size
in the Fortran routine:

float  --- REAL     (this is typical, but not always the case)
double --- REAL*8

The Fortran function must be declared at the beginning of the C calling function:

extern void read_(int *small, float *medium, double *large);

Note we have to pass all variables to Fortran as pointers. Although the function name is not case sensitive in Fortran, it gains an underscore in the C declaration and when it is called:

 read_(&small, &medium, &large);

The Fortran function receives the variables as follows:

SUBROUTINE READ(small,medium,large)

  INTEGER       small
  REAL          medium
  DOUBLE        large

The precise size of these variables depends on your system's architecture (32 bit verses 64 bit), so you need to confirm the correspondence between ints, floats and doubles in C and Fortran on your system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文