Mixed Programming Fortran and C
Asked Answered
A

2

2

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

Asbury answered 12/10, 2011 at 17:6 Comment(2)
What version of Fortran? If it's Fortran 77, you could use f2c.Maddalena
I'm really confused about what is not working for you. Linking object files after the c and fortran bits are compiled separately is the way to combine c and fortran in a single project. Are you looking for an automated header translator?Pronouncement
V
7

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);
  ...
}
Veneaux answered 12/10, 2011 at 18:10 Comment(7)
"What is a Fortran header file ?" In the bad old days it was not uncommon to put COMMON block declarations in a separate file (often with a .inc ending which were then INCLUDEd by multiple files. That's what I think when some someone says "header" in a FORTRAN context. I can't, of course, speak for anyone else.Pronouncement
By fortran header file I mean a file with a *.h extension which contains segments of header file. For example, the following is a code form the header file which is written in fortran, *** c----------------------------------------------------------------------c c derived from dssusy.h 2008-01-22, paolo gondolo * useful global variables real*8 pi real*8 gev2cm3s ! added by /tb common /ruseful/ pi, gev2cm3s c save common blocks save /ruseful/Asbury
And yes, I am using GNU C compiler and GfortranAsbury
Again please, do not include a Fortran include file into a C file ! NEVER ! This cannot work. Fortran include files have to be included into Fortran source files only.Veneaux
I understand, that but there are certain files in the library which have a .h extension and they are called within the library frequently and using Fortran.Asbury
So, your problem is that your c compiler won't process fortran, right? Well, yeah. The c compiler and the fortran compiler are separate things because they accept different languages. (Note that the .h ending of c headers is purely a human convention that the compiler knows nothing about...giving a file a .h ending does not mean the c compiler will magically be able to make heads or tails of it) Perhaps you want a pre-processing stage that can prepare c headers that describe a fortran interface. That's a interesting request. Alas, I don't know of a tool to do that.Pronouncement
"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." Yes, C and Fortran are different languages and you cannot expect a C compiler to understand Fortran source code, or vice-a-versa. If you have some declaration of variables in Fortran and C needs to understand those same variables, you will have to write an equivalent declaration in C.Timmerman
L
4

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.

Loredo answered 12/10, 2011 at 17:10 Comment(6)
1) The code I am trying to call is not a function it is a routine. 2) The errors occur when it parses the include command and tries to compile the header files.Asbury
Hi,I understand that too. But the problem occurs when the compiler goes to the header file which is written in Fortran but I compile with GCC C. I could try to convert the .h file into object but there are numerous such file with intricate dependencies. I do not think this is a very good idea. That is why iam looking for alternatives.Asbury
Compile your C code with gcc and compile your Fortran code with gfortran. Linking with gfortran appears to deal with any run-time library issuesLoredo
gfortran -o myprog myprog.o read.o, gcc -c myprog.c, gfortran -c read.fLoredo
here are numerous such file with intricate dependencies. I do not think this is a very good idea. That is why iam looking for alternatives. –Asbury
use both gcc and gfortan compiler for different c and fortune language fileLoredo

© 2022 - 2024 — McMap. All rights reserved.