This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
Re: error calling c functions from gfortran
- From: FX Coudert <fxcoudert at gmail dot com>
- To: mike dot howell at mchsi dot com
- Cc: fortran at gcc dot gnu dot org (gfortran mail list)
- Date: Sat, 10 Feb 2007 17:57:38 +0100
- Subject: Re: error calling c functions from gfortran
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:in-reply-to:references:mime-version:content-type:message-id:cc:content-transfer-encoding:from:subject:date:to:x-mailer; b=nAINiHZ/fJREz5vzo8wtVdDQ6JJkVvyc2IZqGvcDTe1unaHmzq5naiAgM4DIu2u2XQPcvFmXRLYEpVwB3rt7amjLjYpCiNBXUeRNW72w+INXu5Ef8Cvj5HvUVFmBbyim8wRTGZvPYJCP877GwBlyM3Cikzq59ZOfUKnW+RqUkzA=
- References: <021020071647.18050.45CDF725000DCC4C00004682219791336303010CD2079C080C03BF04040A990108D20A050703@mchsi.com>
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