|
Prev: f77 nostalgia
Next: Derived types and allocatable
From: K-9 on 5 May 2008 22:55 Hi guys....I've spent the whole night trying to figure out about this question. Please help me if you have any clue. Thanks a lot. The code is very easy. I'm trying to call a fortran function from C. The code is as follow: =========C code========= #include "stdio.h" extern float r_ (int * d, float *b); main () { float a = 1.5, b = 1.05, c = 1; int d = 2; c = r_ (&d, &b); printf ("%f\n", c); } ==========Fortran Code=========== real function r(m,t) integer m real t print *, m, t r = 0.1*t+46 print *, r return end ================================ It doesn't matter what the fortran code does...but when I compile it with g77, the result can't be passed back to the C code, but with gfortran, it can. Here are the outputs: ***fortran code compiled by gfortran**** [du(a)brutus qr]$ gfortran -funroll-all-loops -O3 -c ceiling.f -o ceiling.o;gcc -funroll-all-loops -O3 -c testint.c -o testint.o;g77 -o testint.x testint.o ceiling.o -L/pkgs/intel/mkl/9.0/lib/em64t -L/home/ du/lib/hpl/lib/LINUX_XEON_QUAD -L/usr/lib/gcc/x86_64-redhat-linux/ 3.4.6/ -L/usr/lib/gcc/x86_64-redhat-linux/4.1.1 -L. -lgfortran [du(a)brutus qr]$ ./testint.x 2 1.050000 46.10500 <----------- result in Fortran function 46.105000 <--------- result received by C code ***fortran code compiled by g77**** [du(a)brutus qr]$ g77 -funroll-all-loops -O3 -c ceiling.f -o ceiling.o;gcc -funroll-all-loops -O3 -c testint.c -o testint.o;g77 -o testint.x testint.o ceiling.o -L/pkgs/intel/mkl/9.0/lib/em64t -L/home/ du/lib/hpl/lib/LINUX_XEON_QUAD -L/usr/lib/gcc/x86_64-redhat-linux/ 3.4.6/ -L/usr/lib/gcc/x86_64-redhat-linux/4.1.1 -L. -lgfortran [du(a)brutus qr]$ ./testint.x 2 1.04999995 46.1049995 -0.000000 <----------- but here I get 0, with the only difference in the way the fortran code is compiled. Here are the versions: [du(a)brutus qr]$ g77 -v Reading specs from /usr/lib/gcc/x86_64-redhat-linux/3.4.6/specs Configured with: ../configure --prefix=/usr --mandir=/usr/share/man -- infodir=/usr/share/info --enable-shared --enable-threads=posix -- disable-checking --with-system-zlib --enable-__cxa_atexit --disable- libunwind-exceptions --enable-languages=c,c++,f77 --disable-libgcj -- host=x86_64-redhat-linux Thread model: posix gcc version 3.4.6 20060404 (Red Hat 3.4.6-4) [du(a)brutus qr]$ gfortran -v Using built-in specs. Target: x86_64-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man -- infodir=/usr/share/info --enable-shared --enable-threads=posix -- enable-checking=release --with-system-zlib --enable-__cxa_atexit -- disable-libunwind-exceptions --enable-libgcj-multifile --enable- languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk -- disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2- gcj-1.4.2.0/jre --with-cpu=generic --host=x86_64-redhat-linux Thread model: posix gcc version 4.1.2 20070626 (Red Hat 4.1.2-13) [du(a)brutus qr]$ gcc -v Using built-in specs. Target: x86_64-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man -- infodir=/usr/share/info --enable-shared --enable-threads=posix -- enable-checking=release --with-system-zlib --enable-__cxa_atexit -- disable-libunwind-exceptions --enable-libgcj-multifile --enable- languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk -- disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2- gcj-1.4.2.0/jre --with-cpu=generic --host=x86_64-redhat-linux Thread model: posix gcc version 4.1.2 20070626 (Red Hat 4.1.2-13) THANKS AGAIN FOR THE HELP!
From: e p chandler on 5 May 2008 23:26 On May 5, 10:55 pm, K-9 <rick.peng...(a)gmail.com> wrote: > Hi guys....I've spent the whole night trying to figure out about this > question. Please help me if you have any clue. Thanks a lot. > > The code is very easy. I'm trying to call a fortran function from C. > > The code is as follow: > > =========C code========= > #include "stdio.h" > > extern float r_ (int * d, float *b); > > main () > { > float a = 1.5, b = 1.05, c = 1; > int d = 2; > c = r_ (&d, &b); > printf ("%f\n", c); > > } > > ==========Fortran Code=========== > real function r(m,t) > integer m > real t > > print *, m, t > r = 0.1*t+46 > print *, r > > return > end > ================================ > > It doesn't matter what the fortran code does...but when I compile it > with g77, the result can't be passed back to the C code, but with > gfortran, it can. > [du(a)brutus qr]$ g77 -v ... > gcc version 3.4.6 20060404 (Red Hat 3.4.6-4) > [du(a)brutus qr]$ gfortran -v ... > gcc version 4.1.2 20070626 (Red Hat 4.1.2-13) > > [du(a)brutus qr]$ gcc -v ... > gcc version 4.1.2 20070626 (Red Hat 4.1.2-13) 1. Your g77 and gcc are of two different generations. 2. At some point g77 diverged from gfortran. They no longer (IIRC) have the same calling conventions. 3. Who cares? Is there a compelling reason to use g77, which is no longer actively maintained, instead of gfortran, which is under active development?
From: K-9 on 5 May 2008 23:35 Good question. I'm trying to use g77 because i'm trying to develop some fortran77-only code. Of course yes i can go with gfortran. I was just curious to see the reason. Thanks. On May 5, 11:26 pm, e p chandler <e...(a)juno.com> wrote: > On May 5, 10:55 pm, K-9 <rick.peng...(a)gmail.com> wrote: > > > > > > > > > Hi guys....I've spent the whole night trying to figure out about this > > question. Please help me if you have any clue. Thanks a lot. > > > The code is very easy. I'm trying to call a fortran function from C. > > > The code is as follow: > > > =========C code========= > > #include "stdio.h" > > > extern float r_ (int * d, float *b); > > > main () > > { > > float a = 1.5, b = 1.05, c = 1; > > int d = 2; > > c = r_ (&d, &b); > > printf ("%f\n", c); > > > } > > > ==========Fortran Code=========== > > real function r(m,t) > > integer m > > real t > > > print *, m, t > > r = 0.1*t+46 > > print *, r > > > return > > end > > ================================ > > > It doesn't matter what the fortran code does...but when I compile it > > with g77, the result can't be passed back to the C code, but with > > gfortran, it can. > > [du(a)brutus qr]$ g77 -v > ... > > gcc version 3.4.6 20060404 (Red Hat 3.4.6-4) > > [du(a)brutus qr]$ gfortran -v > ... > > gcc version 4.1.2 20070626 (Red Hat 4.1.2-13) > > > [du(a)brutus qr]$ gcc -v > ... > > gcc version 4.1.2 20070626 (Red Hat 4.1.2-13) > > 1. Your g77 and gcc are of two different generations. > 2. At some point g77 diverged from gfortran. They no longer (IIRC) > have the same calling conventions. > 3. Who cares? Is there a compelling reason to use g77, which is no > longer actively maintained, instead of gfortran, which is under active > development?
From: Greg Lindahl on 6 May 2008 00:22 In article <5065dea5-eb9e-4b0c-9413-04252b88eff0(a)34g2000hsh.googlegroups.com>, K-9 <rick.peng.du(a)gmail.com> wrote: >Good question. I'm trying to use g77 because i'm trying to develop >some fortran77-only code. Of course yes i can go with gfortran. I was >just curious to see the reason. Check out the f2c-abi. On x86 and x86-64, this is the difference between g77 and gfortran's calling conventions. -- greg
From: Charles Coldwell on 6 May 2008 23:10
lindahl(a)pbm.com (Greg Lindahl) writes: > In article <5065dea5-eb9e-4b0c-9413-04252b88eff0(a)34g2000hsh.googlegroups.com>, > K-9 <rick.peng.du(a)gmail.com> wrote: > >>Good question. I'm trying to use g77 because i'm trying to develop >>some fortran77-only code. Of course yes i can go with gfortran. I was >>just curious to see the reason. > > Check out the f2c-abi. On x86 and x86-64, this is the difference between > g77 and gfortran's calling conventions. In that case, does this declaration fix the OP's code: extern double r_ (int * d, float *b); i.e., in the C code, declare the return value to be double, but maintain the "real function r(m,t)" definition in the Fortran code compiled by g77. Chip -- Charles M. "Chip" Coldwell "Turn on, log in, tune out" GPG Key ID: 852E052F GPG Key Fingerprint: 77E5 2B51 4907 F08A 7E92 DE80 AFA9 9A8F 852E 052F |