How to interact with fortran code

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 10 05:12:19 PDT 2014


On Wednesday, 9 July 2014 at 15:09:08 UTC, Chris wrote:
> On Wednesday, 9 July 2014 at 15:00:25 UTC, seany wrote:
>> I apologize many times for this question, may be this had 
>> already been answered somewhere, but considering today the 
>> last of my nerve is broken, I can not really find the soution.
>>
>> So I have a D code, which acts as a central manager of all my 
>> codes, reads user input, reads files, etc, and based on the 
>> file readouts, I would like to pass some variables from the D 
>> code to a fortran code, in binary format, perhaps, if such a 
>> thing exists, instead of encoding to text/ ASCII first.
>>
>> I would also like to read some (not all) variables back from 
>> the fortran code.
>>
>> The Fortran code resides in a subdirectory to the 
>> path/to/d/code
>>
>> How to do this? is there a preffered way / easier than system 
>> call way to interface D and Fortran code? This must be Fortan 
>> code - these are the standard atmospheric chemistry codes.
>>
>> I apologize again if the question is stupid, trust me, today 
>> all my nerves are broken.
>
> Off the top of my head I'd say you could try to interface 
> Fortran and C. Then you could interface D and C, i.e. D > C > 
> Fortran.
>
> http://fortranwiki.org/fortran/show/Generating+C+Interfaces
> https://gcc.gnu.org/onlinedocs/gcc-3.4.4/g77/C-Interfacing-Tools.html

To expand on that:

You don't actually need to write a C glue layer between D and 
Fortran. All you need to do is make your Fortran functions 
accessible for C code. As I'm not familiar with Fortran, I don't 
know how exactly that works, but it could involve telling the 
compiler to use the right calling convention, and use the right 
name mangling.

On the D side, you can then declare and use the Fortran functions 
as follows:

     extern(C) float my_fortran_func(int a, int b);

     void bar() {
         writeln("Fortran returned: ", my_fortran_func(10, 20));
     }

Here is an article that describes how to interface with C code 
from D:
http://wiki.dlang.org/Bind_D_to_C


More information about the Digitalmars-d-learn mailing list