Fortran module combine with procedure being accessed in C and vice versa -


i trying test fortran/c mixed language using module , procedure. used base example case link: http://cftcc.iccas.ac.cn/upload/doc/ifc/f_ug1/pgwusmod.htm when try modify code, start error

"_initfo_", reference from: _main__ in main.o ld: symbol(s) not found architecture x86_64.  

here code:

new.f >>       module examp       use iso_c_binding          real, bind(c) :: a(3)          integer i1, i2          character(80) line          type mydata              sequence              integer n              character(30) info           end type mydata       end module examp cnew.c >> /* c  code accessing module data */ extern float a[3]; extern int examp_mp_i1, examp_mp_i2; extern char examp_mp_line[80]; //extern void usemodule(); extern struct {     int n;     char info[30]; } examp_mp_mydata;  void pythagoras (float *c){     *c = (float) sqrt(a[0]*a[0] + a[1]*a[1]); }  void initfo(float *aa){      *aa = a[0]+a[1]+a[2]; } main.f >> ! fortran 95/90 module including procedure       module cproc         interface             subroutine pythagoras ( res)             !dec$ attributes c :: pythagoras             !dec$ attributes reference :: res ! res passed reference because individual attribute !: overrides subroutine's c attribute             real res ! , b have value attribute default because ! subroutine has c attribute             end subroutine         end interface         end module  ! fortran 95/90 module including procedure       module ccproc         interface             subroutine initfo (aa)             real aa             end subroutine         end interface         end module          program main         use examp ! fortran 95/90 module including procedure         use cproc         use ccproc         a(1)=1.0         a(2)=2.0         a(3)=3.0         write(*,*) a(1)         call pythagoras ( x)         write(*,*) x          call initfo(y)         write(*,*) y         end program main 

i using intel compilers. did compile:

icc -c cnew.c ifort -c new.f ifort -o test main.f new.o cnew.o 

i new fortran. hope can point me right direction.

thanks,

jing

with fortran 2003 declare interfaces this:

  interface       subroutine pythagoras (res) bind(c, name='pythagoras')         use iso_c_binding         real(kind=c_float) :: res       end subroutine        subroutine initfo (aa) bind(c, name='initfo')         use iso_c_binding         real(kind=c_float) :: aa       end subroutine   end interface 

the main point here name attribute added subroutine declaration, tells compiler, symbol use. otherwise name mangling gives not found entities.

in total gives: new.f90

module examp   use iso_c_binding    real(kind=c_float), bind(c) :: a(3)   integer :: i1, i2   character(80) :: line    type mydata      sequence      integer :: n      character(len=30) :: info   end type mydata  end module examp 

main.f90:

module cproc   use iso_c_binding   interface       subroutine pythagoras (res) bind(c, name='pythagoras')         use iso_c_binding         real(kind=c_float) :: res       end subroutine        subroutine initfo (aa) bind(c, name='initfo')         use iso_c_binding         real(kind=c_float) :: aa       end subroutine   end interface end module  program main    use examp ! fortran 95/90 module including procedure    use cproc     a(1)=1.0    a(2)=2.0    a(3)=3.0    write(*,*) a(1)    call pythagoras(x)    write(*,*) x     call initfo(y)    write(*,*) y end program main 

with c-code unchanged. urge not use global variables a.


Comments