Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!usc!apple!amdahl!key!sjc From: sjc@key.COM (Steve Correll) Newsgroups: comp.lang.fortran Subject: Re: Calling a C function from Fortran. Message-ID: <2025@key.COM> Date: 3 Aug 90 23:03:11 GMT References: <1990Aug2.191231.11783@portia.Stanford.EDU> Organization: Key Computer Labs, Fremont, CA Lines: 51 In article <1990Aug2.191231.11783@portia.Stanford.EDU>, volovich@dendrite.Stanford.EDU (Gene Volovich) writes: > I'm calling a C function from a FORTRAN program on an RT running AOS, and > using the standard C compiler and f77. I actually got this to run, but > my question is this: why does there need to be an underscore after the name > of the C function I'm calling? Here's my code: The Fortran 77 standard permits a program to use an intrinsic even though it contains a user-coded function with the same name: logical function sin() sin = .true. end C This subroutine calls the user-written logical function "sin" subroutine s() logical sin external sin print *, sin() .eqv. .true. end C This subroutine calls the trig intrinsic "sin" which already C exists in Unix libm under the name "sin" subroutine t() print *, sin(1.0) end The Unix Fortran implementors were faced with two conflicting requirement: 1. They wanted to be able to use existing libm routines like "sin", which were originally invented for use with C, to provide the trig intrinsics. 2. They wanted to emit something close to "sin" to implement the user-coded function "sin". They chose to solve this by appending "_" to all user-written routine names. On many operating systems, the problem doesn't occur because the intrinsics in the libraries have names containing $ or other unusual characters so as not to conflict with user-written routines. Actually, just to confuse things, the Unix Fortran library offers yet another class of names. The intrinsics in libm take their arguments by value (which usually enhances execution speed), but when you pass an intrinsic as an actual argument, the compiler must supply a wrapper which takes its argument by reference in the standard Fortran fashion. Unix implementations may vary in this regard, but the SunOS implementation uses a _r_ prefix, e.g. _r_sin. C This subroutine passes the intrinsic "sin" as an argument. subroutine u() intrinsic sin call foo(sin) end -- ...{sun,pyramid}!pacbell!key!sjc Steve Correll