views:

78

answers:

2

Hi! I'm trying to make a Fortran 77 wrapper for C++ code. I have not found information about it. The idea is to use from functions from a lib that is written in C++ in a Fortran 77 progran.

Does anyone know how to do it? Thanks!

A: 

Calling Fortran from C is easy, C from Fortran potentially tricky, C++ from Fortran may potentially become ... challenging.

I have some notes elsewhere. Those are quite old, but nothing changes very rapidly in this sort of area, so there may still be some useful pointers there.

Unfortunately, there's no really standard way of doing this, and different compilers may do it slightly different ways. Having said that, it's only when passing strings that you're likely to run into major headaches. The resource above points to a library called CNF which aims to help here, mostly by providing C macros to sugar the bookkeeping.

The short version, however is this:

  • Floats and integers are generally easy -- an integer is an integer, more or less.
  • Strings are hard (because Fortrans quite often store these as structures, and very rarely as C-style null-terminated arrays).
  • C is call-by-value, Fortran call-by-reference, which means that Fortran functions are always pointer-to-value, from C's point of view.
  • You have to care about how your compiler generates symbols: compilers often turn C/Fortran symbol foo into _foo or foo_ or some other variant (see the compiler docs).
  • C tends not to have much of a runtime, C++ and Fortran do, and so you have to remember to link that in somehow, at link time.

That's the majority of what you need to know. The rest is annoying detail, and making friends with your compiler and linker docs. You'll end up knowing more about linkers than you probably wanted to.

Norman Gray
+1  A: 

Lawrence Livermore National Laboratory developed a tool called Babel for integrating software written in multiple languages into a single, cohesive application. If your needs are simple you can probably just put C wrapper on your C++ code and call that from Fortran. However, if your needs are more advanced, it might be worth giving Babel a look.

Rakis