Mixed language (Fortran and C) programming?

homercles337

Diamond Member
Dec 29, 2004
6,340
3
71
Has any one done any Fortran and C coding? I have zero experience with Fortran. Im using Code::Blocks in Debian and have no clue how to even get a mixed language module to compile. In theory, i do not need to modify the Fortran code, since C is calling Fortran, but i do need to modify the C. All of my googling has turned up very little. There is a forked Fortran IDE (forked from Codeblocks), but nothing for mixed language coding.
 

homercles337

Diamond Member
Dec 29, 2004
6,340
3
71
Thanks for the links. I installed and tried to use f2c, but it failed miserably, as did cfortran.h. I also tried gfortran, f77, and gcc, on single fortran files, they all failed until i discovered that these flags were important:

-O -ffixed-line-length-132 -fcray-pointer

No clue what they do, but once i have my .o files cant i just link them in with the C compiler manually?
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Thanks for the links. I installed and tried to use f2c, but it failed miserably, as did cfortran.h. I also tried gfortran, f77, and gcc, on single fortran files, they all failed until i discovered that these flags were important:

-O -ffixed-line-length-132 -fcray-pointer

No clue what they do, but once i have my .o files cant i just link them in with the C compiler manually?

Unfortunately, no (at least not easily). The problem is with the calling conventions. Fortran uses an interesting calling convention (all data is passed by reference instead of being passed by value).

You'll want to look into and figure out if your fortran compiler uses STDCALL (everything is passed on the stack.) or some other calling convention. That is always fun to deal with. Other gotchas involve things like array organization and use and just datatype use in general (the document I linked talks about this.)

Once you figure all of that out, then the linking will essentially be you putting in the prototypes for each of the fortran functions you want to use (making sure things like every variable passed in is a pointer).
 

homercles337

Diamond Member
Dec 29, 2004
6,340
3
71
As always things are much more complicated than originally thought. I get massive linker errors now, all are "undefined reference to '_gfortran_*'" Wildcard examples are "st_write" or "transfer_integer" or "malloc" or "free" etc. Yetch...

EDIT: I added the -lgfortran flag to the linker, now its all good. Well, it compiled, not clue if it runs as expected...
 
Last edited: