This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
Bizarre error with mixed C and Fortran
- From: Bernd Lütkenhöner <bernd dot lutkenhoner at uni-muenster dot de>
- To: <fortran at gcc dot gnu dot org>
- Date: Thu, 16 Jul 2009 11:01:49 +0200 (CEST)
- Subject: Bizarre error with mixed C and Fortran
Dear All,
After a system upgrade, I had to replace the G77 compiler by GCC
(version 4.3.2). Unfortunately, some programs show bizarre errors now.
I finally managed to isolate one of these errors and to reproduce
it in a very simple test environment, consisting of one C and
one Fortran module.
Here is a scheme of the calling sequence (routines F* written
in Fortran, routines C* written in C):
main() ---> F1 ---> C1 ---> F2 ---> CX
The output generated by routine C1 is:
1.000000 2.000000 3.000000 0.000000 0.000000 0.000000 -0.000000
nan 2.000000 3.000000 0.000000 0.000000 0.000000 -0.000000 -0.000000
1.000000 2.000000 3.000000 0.000000 0.000000 0.000000 -0.000000 -0.000000
Note the NaN in the second line (the last two lines should be
identical).
If routine CX is replaced by FORTRAN routine FX, i.e.,
main() ---> F1 ---> C1 ---> F2 ---> FX
the error disappears. There seems to be a bug either in the compiler
or the linker. Any workaround (e.g. a compiler option)?
Many thanks,
Bernd
########################################################################
Here are the two modules (these two files, and the Makefile, could
also be made available in a TAR or ZIP file).
/***************************************/
/* The C module */
/***************************************/
#include <stdio.h>
#include <stdlib.h>
extern void f1_(void);
extern void f2_(double *r);
int main(void) {
f1_(); /* FORTRAN routine */
}
void c1_(void) /* called by FORTRAN routine f1 */
{
double r[3],x[5];
f2_(r); /* FORTRAN routine */
/* works fine (uncommenting has no influence on subsequent error) */
printf ("%f %f %f %f %f %f %f\n",
r[0],r[1],r[2],x[0],x[1],x[2],x[3]);
/*!!!!!! prints NaN for r[0] !!!!!!*/
printf ("%f %f %f %f %f %f %f %f\n",
r[0],r[1],r[2],x[0],x[1],x[2],x[3],x[4]);
/* works fine (same statement as before, just called a second time) */
printf ("%f %f %f %f %f %f %f %f\n",
r[0],r[1],r[2],x[0],x[1],x[2],x[3],x[4]);
}
double cx_(double *r) /* called by FORTRAN routine f2 */
{
r[0] = 1;
r[1] = 2;
r[2] = 3;
}
c***************************************/
c* The FORTRAN module */
c***************************************/
subroutine f1
call c1
return
end
SUBROUTINE f2 (r)
REAL*8 r(3)
c calling C-routine cx causes a problem,
call cx (r)
c no problem if routine fx (see below) is called instead of C-routine cx
c call fx (r)
return
END
SUBROUTINE fx (r)
REAL*8 r(3)
r(1) = 1
r(2) = 2
r(3) = 3
return
end
########################################################################
Finally, some information about the version of GCC that I used:
>gcc -v
Using built-in specs.
Target: i586-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info
--mandir=/usr/share/man --libdir=/usr/lib --libexecdir=/usr/lib
--enable-languages=c,c++,objc,fortran,obj-c++,java,ada
--enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.3
--enable-ssp --disable-libssp --with-bugurl=http://bugs.opensuse.org/
--with-pkgversion='SUSE Linux' --disable-libgcj --disable-libmudflap
--with-slibdir=/lib --with-system-zlib --enable-__cxa_atexit
--enable-libstdcxx-allocator=new --disable-libstdcxx-pch
--enable-version-specific-runtime-libs --program-suffix=-4.3
--enable-linux-futex --without-system-libunwind --with-cpu=generic
--build=i586-suse-linux
Thread model: posix
gcc version 4.3.2 [gcc-4_3-branch revision 141291] (SUSE Linux)