混合编程Fortran和C [英] Mixed Programming Fortran and C

查看:175
本文介绍了混合编程Fortran和C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我已经能够在众多测试文件中链接这两个程序,并且它们的工作非常出色。但对他们来说,我一直在使用目标文件来链接它们。但是当我试图通过C来运行真正的交易时,请包含对Fortran头文件的引用。他们似乎集成并调用了对方,但Fortran头文件的格式与C编译器不兼容,所以当它跳转到头文件时,它会开始抛出无法理解语法的错误。例如,Fortran头文件定义了带有real * 8的双变量,所以当C读取它们时会抛出错误。文件中的注释也是如此。



所以,我想问一下,有什么方法可以解决这个问题?即通过C使fortran格式的头文件可读。



我查看了互联网,发现了令人困惑的答案,我不知道应该遵循哪一个。在这方面的任何帮助将不胜感激:)

解决方案

对不起,但你很混乱。什么是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



<例如:

  void my_sub(int,double *); 





  #includemy_sub.h

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


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 :)

解决方案

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);
  ...
}

这篇关于混合编程Fortran和C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆