Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

c++ call fortran subroutine

qiu_n_
Beginner
1,051 Views

i have code ,i use C call fortran subroutine,  i send same parameter to fortran subroutine, one of parameter is c++ struct,in the struct have a float pointer ,i want use this pointer in fortran subrountine ,how can i use?

following code is  main programe (c language) 

#include <iostream>
#include <string.h>
struct header {
int head_length;
int trace_length;
int cmpline;
int cmp;
float rx;
float ry;
float *data;
};


using namespace std;
extern "C" 
{
    void fr1_(int *,int *,char * ,header *); 
}
int main()
{
int n=10,nSq;
char szCtest[20];
struct header hd;

hd.head_length=192;
hd.trace_length = 5000;
hd.cmpline = 1000;
hd.cmp =  20;
hd.rx = 1111.0;
hd.ry = 3333.0;

hd.data = new float[hd.trace_length];

for (int i=0;i<hd.trace_length;i++) {
    hd.data = i;
}

for (int i=0;i<10;i++) {
    cout <<"i:"<<hd.data<<endl;
}
 strcpy(szCtest,"teststring");
fr1_(&n,&nSq,szCtest,&hd);
cout << "The square is:" << nSq << endl;
return 0;

}

and following code is  fortran :


      subroutine fr1(n,m,cstr,hd)
      use iso_C_binding
      implicit none
      integer,intent(in)::n
      integer,intent(out)::m
      integer,intent(in)::cstr(1)
      integer::l,index

!      real,pointer ::trace_data
!     here we receive the c char string in an integer array

!     could also have used a byte array

      type header  
      integer::head_length
      integer::trace_length
      integer::cmpline
      integer::cmp
      real::rx
      real::ry
      real,pointer :: data(:)
      end type 
      type(header),intent(inout):: hd

      m = n*n

      write(6,20) (cstr(l),l=1,3)
   20 format(' cstr=',3a4)

!      ALLOCATE(trace_data)

!      trace_data=>hd%data

      write(*,*) 'head_length:',hd%head_length
      write(*,*) 'trace_length:',hd%trace_length
      write(*,*) 'cmpline:',hd%cmpline
      write(*,*) 'cmp:',hd%cmp
      write(*,*) 'rx:',hd%rx
      write(*,*) 'ry:',hd%ry

      do index = 1,9,1
         write(*,*) "trace_data :",hd%data(index)

      enddo

      write(6,*) 'done'

!      deallocate(trace_data)

      return

      end

the program run  error message is following:

i:0
i:1
i:2
i:3
i:4
i:5
i:6
i:7
i:8
i:9
 cstr=teststring
 head_length:         192
 trace_length:        5000
 cmpline:        1000
 cmp:          20
 rx:   1111.000    
 ry:   3333.000    
Segmentation fault (core dumped)

compiler set:

icc -c test.cpp 
ifort -std -nofor_main -cxxlib -fexceptions -o myprog $1 test.o

please answer my question , thank  very much!

 

 

0 Kudos
3 Replies
TimP
Honored Contributor III
1,051 Views

You must declare the C pointer as c_ptr, not pointer.  Then you may use the c_f_pointer intrinsic to make a Fortran pointer from it.

0 Kudos
qiu_n_
Beginner
1,051 Views

thank you vary much . it's ok。

0 Kudos
Yue__Zhiying
Beginner
1,051 Views

I am a beginner of C++ and Linux. I wonder that could you please describe your detailed process that you deal with this problem? For examle, how do you compile your fortran files and what prerequrisites needed in this process? I also want to learn the way that using C++ to call fortran. Thank you very much.

0 Kudos
Reply