Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!ucbvax!hplabs!hpfcdc!hpfcmgw!bobm From: bobm@hpfcmgw.HP.COM (Bob Montgomery) Newsgroups: comp.sys.hp Subject: Re: C calls to Fortran subroutines on HP9000/300 Message-ID: <1080053@hpfcmgw.HP.COM> Date: 25 May 89 18:54:24 GMT References: <436a0d6e.d498@mets.engin.umich.edu> Organization: HP Fort Collins, CO Lines: 71 > I am trying to call Fortran subroutines from a C main > program, but don't know how to set up the call. I can't find > any preprocessor commands like ALIAS which you can use when calling > C from a Fortran program. The Fortran routines I want > to call are part of a general environment graphics package. C has a lot more flexibility than FORTRAN, so the functionality of ALIAS is not usually required. Specifically, C has pointers so you can pass the addresses of variables to FORTRAN routines, since FORTRAN expects parameters to be passed by reference. C has case-sensitive names, so you can match the name requirements of the FORTRAN routines, (usually all lower-case, unless the -U (uppercase) option was used to compile the FORTRAN code; running nm(1) on the FORTRAN objects will tell you which it is.) The Series 300 uses identical naming rules for FORTRAN and C (i.e. no trailing underscore for FORTRAN names). As long as you are only dealing with integer and real scalars and array parameters, just remember to pass pointers from C and it's no big deal. If you need to pass or accept returned character strings, please post an example of what is required by your library. If the FORTRAN application library expects to do its own FORTRAN IO, you may need to start your program as a FORTRAN main that immediately calls your C main code so the FORTRAN IO library can be initialized correctly. Other details are included in a manual called the HP-UX Portability Guide (HP Part Number 98794-90046). There are sections on calling C from FORTRAN and on calling FORTRAN from C. You should read both sections, since some of the material that applies in both directions does not appear in the FORTRAN from C section. Bob Montgomery Workstations, Support, and Big Deals HP P.S. An example: cmain.c: float arr[100]; main() { int i; float sum, sumv(); for (i=0; i<100; i++) arr[i] = (float)i; i = 100; /* pass address of i instead of i */ sum = sumv(arr,&i); printf("sum: %f\n", sum); } --------------------------------------------- fsub.f: real*4 function sumv(v, n) integer i, n real*4 v(n) real*4 sum sum = 0.0 do i=1,n sum = sum + v(i) end do sumv = sum end --------------------------------------------- Output: sum: 4950.000000