linking c and fortran in the same archive

Jean-François MAUREL jfr.maurel@gmail.com
Thu Nov 12 15:29:00 GMT 2009


Hi every one,

I am a newcomer to gcc. I searched within documentation and mailing list 
archive but did not find anything useful for the following issue:
I cannot link c and f object files when I provide a single archive file. 
I can do it only when I provide individual object files

Below are the details:

I can compile link and execute a test program containing a c program 
file and a fortran program file (see below) with following set of commands:
 (Windows Vista MinGW gcc , gfortran 4.4.0 ):
gcc -c main.c -omain.o -Wall
gfortran -c sub.f -osub.o -Wall
gcc *.o  -Wall -enable-stdcall-fixup -omain.exe

when I use the following commands I have an undefined reference error :
gcc -c main.c -omain.o -Wall
gfortran -c sub.f -osub.o -Wall
ar -r libtotal.a main.o sub.o
gcc -ltotal  -Wall -enable-stdcall-fixup -omain.exe -L.
./libtotal.a(main.o):main.c:(.text+0x6b): undefined reference to `test_@24'
collect2: ld a retourné 1 code d'état d'exécution

Could someone tell what I am missing here?



main.c
==============
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define INTEGER long
#define STRING_SIZE unsigned int

#define CALL_TEST(a,b,c,d,e) test_(a,b,c,d,e,strlen(b))
void __attribute__((stdcall)) test_(INTEGER *, char *, INTEGER *, double 
*, double *, STRING_SIZE);

int main(int argc, char **argv)
{
   INTEGER ivers, ilog;
   char vdate[11] = "           ";
   int iret=4;
   double res, res2;
   vdate[10] = '\0';

   CALL_TEST(&ivers, &vdate[0], &ilog,&res, &res2);
   printf("RESULTS : %ld / '%s' / %ld / %f / %f\n", ivers, vdate, ilog, 
res, res2);
   if (ivers == 10 && res == 10. && res2 == 5. && strncmp(vdate, 
"01/01/2010", 10) == 0) {
      iret = 0;
   }
   printf("EXIT_CODE=%d\n", iret);
   exit(iret);
}
=============
sub.f
=======
      SUBROUTINE TEST(VERS,DATE,EXPLOI, RES, RES2)

      IMPLICIT NONE  
      INTEGER VERS
      CHARACTER*10 DATE
      LOGICAL      EXPLOI
C
      REAL*8  RES, RES2
C
      VERS = 10
      DATE = '01/01/2010'
      EXPLOI = .TRUE.
C     
      RES = 10.D0
      RES2 = 5.D0
      END




-- 
Jean-François MAUREL
PI Méca
4 rue de l'abreuvoir
92400 Courbevoie France
phone: 33(0)147176513





More information about the Gcc-help mailing list