This is a follow on to my previous question (https://software.intel.com/en-us/forums/topic/534413), I am able to pass a simple data type as a return value, but what I really want to do is pass a user-defined type back to the Fortran as the return value from the C++ code. I have a copy of "Modern Fortran Explained" and it suggests that this is indeed possible, but I am getting a compilation error saying that my derived type definition is unknown at the point where I define my interface to the C++-routine.
Here is a sample Fortran code illustrating what I am trying to do:
program FortranCPPInterop type, bind(c) :: inter_op integer :: i double precision :: j end type inter_op interface function CPP_func(ARG) !DEC$ ATTRIBUTES C, DLLIMPORT, ALIAS:'_CPP_func' :: CPP_func !DEC$ ATTRIBUTES REFERENCE :: ARG TYPE(inter_op) :: CPP_func integer ARG end function CPP_func end interface TYPE(inter_op) :: test test = CPP_func( 3) print*, test%i print*, test%j end program
And for completeness (again) the C++ routine it is calling:
struct interop_type { int i; double j; }; extern "C" { __declspec(dllexport) interop_type CPP_func(int* arg) { interop_type ret_val; ret_val.i = *arg * 4; ret_val.j = *arg / 2.0; return ret_val; } }
This set up works just fine if I tweak the C++ to return a basic type (an int), but when I try to have it return the struct/UDT, I get the following compilation errors:
Source1.f90(12): error #6457: This derived type name has not been declared. [INTER_OP]
Source1.f90(19): error #6303: The assignment operation or the binary expression operation is invalid for the data types of the two operands. [CPP_FUNC]
It seems pretty clear that the second error follows from the first, but I have no idea why I am getting the first error, as my type is defined. Can anyone let me know what I might be missing? Thank you!