I have an old Fortran77 program that calls a C++ function and passes several arra开发者_Go百科ys of values to it (~ 100 individual values total). I'd like to convert the arrays to derived types, to make the code more readable and give names to the individual members of the arrays.
Is it possible to pass a derived type from Fortran to a C++ function?
How does C++ know how the structure looks like? Do I have to define the structure twice (once in Fortran and once in C++), and if yes, is there any automatic way to check that both structures are in sync?
I'm using GCC; I'd obviously have to switch the Fortran code to Fortran95 or higher.
Look into ISO_C_BINDING, the C-Fortran binding that is part of F2003. You can create a derived type with a BIND(C) attribute, which will be compatible with a C struct of the "companion C processor" (in the case of gfortran, gcc).
See chapter 7 in the gfortran manual: http://gcc.gnu.org/onlinedocs/gfortran/Mixed-Language-Programming.html
精彩评论