[Linux] Calling C from Fortran: Function always returns zero?

John H. Chauvin jchauvin@netcom.com
Sat Sep 4 05:55:00 GMT 1999


I have written a very simple fortran program which calls a
function which is written in C. This is a very straight
forward test case for mixed Fortran and C programming
under Linux using g77 and gcc. 

The program passes the number four to the C function where
it is multiple by two and the resultant value is returned to
the fortran program. Unfortunately, the return value as
printed from the fortran routine is always zero. Why is
the return value being lost?

Any hints as to why this is happening?

Thanks,

John Chauvin

Fortran Program (number.f)
-------------------
      program Main
      integer result,number
      
      number = 4
      write(6,'(''number(F)='',I1)') number
      result = testit(number)
      write(6,'(''result(F)='',I1)') result
      stop
      end


C Function (testit.c)
-------------------
#include "stdio.h"

int testit_(number)
int *number;
{    
    int result;
    
    result = *number * 2;
    
    printf("number(C)=%d\nresult(C)=%d\n",*number,result);
    
    return result;
} 


Compile and run Procedure
-------------------
gcc -c testit.c
g77 number.f testit.o -o number
../number


Run Result
-------------------
number(F)=4
number(C)=4
result(C)=8
result(F)=0

--
John H. Chauvin jchauvin@netcom.COM
Netcom - Online Communication Services San Jose, CA


More information about the Gcc-help mailing list