cunning@caen.engin.umich.edu (Gregory Scott Cunningham) (05/24/89)
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.
mlight@hp-ptp.HP.COM (Mike_Light) (05/25/89)
>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. C assumes the caller knows what he is doing when invoking a function. It is up to the programmer to know the interface the called function expects. Fortran has a simple call-convention unless you need to share strings. Most normal data types in fortran are passed by reference, i.e., fortran routines expect to be given a pointer to the integer, real, or complex number. Doing this from a C caller is as simple as: int arg1, arg2 ... ; fortran_func(&arg1, &arg2, ...); Strings are beasts from hell and you don't want to use them between languages because they are manipulated in very different ways. But if you must, be warned that fortran expects TWO parameters when a string parameter occurs; the first parameter is a pointer to the string, and the second is the length of the string PASSED BY VALUE! Fortran would also expect the string to be blank-padded on the right out to the string length. char f_string[80]; sprintf(f_string,"%80s","String Data"); fortran_str(f_string,80); Enjoy! ----------------------------------------------------------------------- Mike Light HP Industrial Applications Center - mlight@hpiacla.HP.COM -----------------------------------------------------------------------
ted@hpwrce.HP.COM ( Ted Johnson) (05/25/89)
>I am trying to call Fortran subroutines from a C main >program, but don't know how to set up the call. Check out the "HP-UX Portability Guide" manual, part # 98794-90046, page 57. It has an example. >I can't find >any preprocessor commands like ALIAS which you can use when calling >C from a Fortran program. You don't need one. -Ted
mike@hpfcdc.HP.COM (Mike McNelly) (05/25/89)
Pick up a copy of the "HP-UX Portability Guide for Series HP 9000 Series 300/800 Computers", part number 98794-90046 (for release 6.5). This manual has a chapter on this topic. You might also look at the "HP-UX Assembler Reference and Supporting Documents for HP 9000 Series 300 Computers", part number 98597-90020 (release 6.5) for information about procedure protocols. Mike McNelly mike%hpfcla@hplabs.hp.com
bobm@hpfcmgw.HP.COM (Bob Montgomery) (05/26/89)
> 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