Richard Maine wrote:
Anonymous writes:
I have a mixed-language pgm (Fortran and C++)
which needs to pass a few hundred values from C++ to Fortran.
One way to do this is to have a Fortran module
and a C++ structure (or class) with an identical data layout.
Presumably, you mean a Fortran derived type (aka structure).
Fortran modules don't *HAVE* a data layout;
they are not comparable to C/C++ structures
in any particularly useful ways.
A header file is as close as you can get to a module in C or C++.
I don't have a handy translator for such things (though I'll note
that, in the draft f2003, you can do a Fortran derived type
that is interoperable with a C struct
in a way that is directly comparable enough that
a translator would be simple).
Fortran 90 derived type:
type:: type_t
sequence
integer:: i
real:: x
end type type_t
Equivalent C or C++ structure:
#include <stdint.h>
typedef int32_t f77_integer; // f90 implementation dependent
typedef float f77_single; // f90 implementation dependent
typedef void f77_subroutine; // f90 implementation dependent
typedef struct type_t {
f77_integer i;
f77_single x;
} type_t;
Different Fortran 90 compilers "mangle" the names
of functions contained in modules in different ways
to form the symbols that it leaves behind in object files
for the link editor.
You will need Fortran 90 "helper" functions
to access them reliably.
For example, if the module module_h contains
subroutine f(t)
type(type_t), intent(in):: t
! do something with t
end subroutine f
you will need
subroutine help_f(t)
use module_h
type(type_t), intent(in):: t
f(t)
end subroutine help_f
Then, you can write
#ifdef __cplusplus
extern "C" {
#endif/*__cplusplus */
f77_subroutine f(const type_t* pT) {
extern f77_subroutine help_f_(const type_t*);
help_f_(pT);
}
#ifdef __cplusplus
}
#endif/*__cplusplus */