This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: error calling c functions from gfortran


I get the wrong answer calling c programs from a gfortran program. The same c
functions work when called from a c main program. I guess I need some kind of
interface statement to call c functions from gfortran. Can anyone help me?

A few hints:
1. Fortran passes arguments by reference, so your C functions need to be changed into:


int lshift_(int *Num2Shift, int *NumBits ) { return(*Num2Shift << *NumBits); }
int rshift_(int *Num2Shift, int *NumBits ) { return(*Num2Shift >> *NumBits); }


2. The rshift and lshift in your Fortran code are declared external, but are not given a type. You need to specify that they return integer values, by adding "integer rshift, lshift". Still better, you should ask for implicit typing not to be used (it causes much trouble, especially to beginners), by adding the line "implicit none" just before your type declarations:

      program ingrid
      implicit none
      integer rshift, lshift
      external rshift,lshift
      integer ans
c      read(*,*) i,j
c rshift(i,j): i=# to shift, j=#bits to shift
      ans=rshift(8,1)
      print *,"rshift(8,1)=",ans
      ans=lshift(8,1)
      print *,"lshift(8,1)=",ans
c      write(*,*) ans
      end

With that, it should work:

$ gfortran a.c a.f && ./a.out
rshift(8,1)=           4
lshift(8,1)=          16


FX



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]