Calling f77 subroutines from C main
steve%usna.uucp at BRL-BMD.ARPA
steve%usna.uucp at BRL-BMD.ARPA
Sat Aug 4 11:57:23 AEST 1984
Below are two examples of a C main program calling a f77 subroutine.
Each example has a makefile, a C main and a f77 subroutine. You can
edit this into the appropriate files and try them. We discovered these
techniques by looking through the f77 source and they work for 4.2
and V7.
The normal f77 documentation describes the data types required for
parameter passing when callin C from f77. This description also apples
for the other direction. However, the problem we discovered when we first
called a f77 subroutine from C is not described. The problem is that
I/O in the subroutine will not work unless the proper f77 things are done.
In method 1, the actual 'main' program is supplied by f77 and your
'main' is really called as a function named MAIN_. We generally
use method 2 since it allows normal command line arguements to be used.
These methods work for 4.2 and V7.
Steve Satterfield
Computer Aided Design and Interactive Graphics Group
U. S. Naval Academy
Annapolis, Md. 21402
(301) 267-4413
brl-bmd!usna!steve
*************************************************************************
METHOD 1:
---------- makefile ------------------------------------------------------
main: main.o sub.o
f77 -o main main.o sub.o
---------- main.c ------------------------------------------------------
/* This is a main C routine the calls a f77 subroutine */
MAIN_()
{
int a,b,c;
a = 2;
b = 2;
c = 0;
printf("Calling sub\n");
sub_(&a,&b,&c);
printf("Returned from sub, c = %d\n",c);
}
---------- sub.f ------------------------------------------------------
subroutine sub(a,b,c)
integer a,b,c
c = a+b
write(6,*) 'sub executing'
return
end
*************************************************************************
METHOD 1:
---------- makefile ------------------------------------------------------
main: main.o sub.o
cc -o main main.o sub.o -lF77 -lI77
---------- main.c ------------------------------------------------------
/* This is a main C routine the calls a f77 subroutine */
main()
{
int a,b,c;
f_init(); /* Required f77 initialization */
a = 2;
b = 2;
c = 0;
printf("Calling sub\n");
sub_(&a,&b,&c);
printf("Returned from sub, c = %d\n",c);
f_exit(); /* Required f77 clean up */
}
---------- sub.f ------------------------------------------------------
subroutine sub(a,b,c)
integer a,b,c
c = a+b
write(6,*) "sub executing"
return
end
*********************************************************************
More information about the Comp.lang.c
mailing list