[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A Fortran 77 compiler for Red Hat Linux platforms



Ortal Alpert writes:
 > f2c appear to run as a fortran compiler but can't mix code when main is not in
 > fortran (try C) without writnig libf2c yourself (useualy some 5 lines of code)
 > so it is not the best solution for all problems

Not exactly true.  Consider:

 --- c-main.c ---

   main ()
   {
       int x = 3;

       printf("ftncall(3) = %d\n", ftncall_(&x));
   }

 --- ftncall.f ---

	 integer function ftncall(x)

	 integer x

	 ftncall = x*x+1

	 return
	 end

 --- Compilation & run ---

   hjstein@shalom:~/tmp$ gcc -c -o c-main.o c-main.c
   hjstein@shalom:~/tmp$ f77 -c -o ftncall.o ftncall.f 
      ftncall:
   hjstein@shalom:~/tmp$ gcc -o c-main c-main.o ftncall.o  -lm
   hjstein@shalom:~/tmp$ ./c-main 
   ftncall(3) = 10
   hjstein@shalom:~/tmp$ 

Difficulties only occur when you need *initializations* from libf2c
(i.e. when the Fortran code won't run correctly if libf2c's main()
hasn't been executed).

Note also the following:

   hjstein@shalom:~/tmp$ gcc -o c-main c-main.o ftncall.o  -lf2c -lm
   /usr/lib/libf2c.so: undefined reference to `MAIN__'
   hjstein@shalom:~/tmp$ gcc -static -o c-main c-main.o ftncall.o  -lf2c -lm
   hjstein@shalom:~/tmp$ 

It seems like the stock libf2c shared library is build differently
than the unshared version, so you might need to either rebuild
libf2c.so or link statically in the case where your Fortran code
requires routines from libf2c.

So, yes, it's not the *best* solution for *all* problems.  A modicum
of work may be required if you want to call Fortran routines from a C
program.

Note that the same could hold for other Fortran compilers - they might
also require initializations either from the C library or from the
runtime startup code.  And in the case of other compilers where source
is unavailable, the fix may be nearly unobtainable.

-- 
Harvey J. Stein
Berger Financial Research
abel@netvision.net.il


References: