I have a situation where I want to pass a struct from C++ to Fortran. Every test type (double, int, bool, defined arrays) works except allocatable arrays. But it is strange that they only don't work when there is a string member of the structure. When I remove the string member, it works. Here is my code:
C++:
#include <iostream>
#include <cstddef>
#include <vector>
using namespace std;
struct t_nwork {
double treal;
int tint;
bool tlog;
double trealarr[10];
string tchar;
double *trealalloc;
};
//Fortran subroutine definition
extern "C" {
void nf90(t_nwork *net);
}
int main()
{
t_nwork net;
net.tchar = "Dick";
net.trealalloc = new double[5];
for (int i = 0; i < 5; i++ ){
net.trealalloc[i] = (double) i * 3;
}
nf90(&net);
}
Fortran:
module netf90_mod
implicit none
type :: t_nwork
real(8) :: treal
integer :: tint
logical :: tlog
real(8) :: trealarr(10)
character(4) :: tchar
real(8), allocatable :: trealalloc(:)
end type
contains
subroutine nf90(net) bind(c)
use iso_c_binding
type(t_nwork), intent(inout) :: net
write(6,*) net%tchar
write(6,*) size(net%trealalloc)
write(6,*) net%trealalloc(0)
write(6,*) net%trealalloc(1)
write(6,*) net%trealalloc(2)
write(6,*) net%trealalloc(3)
write(6,*) net%trealalloc(4)
end subroutine
end module
If I comment out the string (tchar) member in all occurrences then the allocatable array gets passed through ok (well the length is 0 but the 5 values come through). But with it in there, it does not work.
I compile and link under Intel:
icx -c testc.cpp
ifort testc.obj testf.f90
I'm no CPP expert, so maybe I haven't got the syntax quite correct.