Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!ames!sgi!bron@bronze.SGI.COM From: bron@bronze.SGI.COM (Bron Campbell Nelson) Newsgroups: comp.lang.fortran Subject: Re: Calling FORTRAN from C (messing with the namespace) Summary: Part of the reason is to *preserve* names Message-ID: <32033@sgi.SGI.COM> Date: 4 May 89 17:11:45 GMT References: <2846@tank.uchicago.edu> <5785@cbnews.ATT.COM> <10087@smoke.BRL.MIL> <11569@cgl.ucsf.EDU> Sender: daemon@sgi.SGI.COM Organization: Silicon Graphics, Inc., Mountain View, CA Lines: 57 In article <11569@cgl.ucsf.EDU>, seibel@cgl.ucsf.edu (George Seibel) writes: > In article <25509@amdcad.AMD.COM> tim@amd.com (Tim Olson) writes: > > [... responding to comments on underscores getting added to names ...] > > >If an underscore is not prepended (or the names changed in some standard > >unique way), then you run into the problem of potential name-space > >conflicts with the assembly-level "helper" routines that almost always > >exist. These routines include "start" (the beginning of crt0, which > >gets control and sets everything up for your program), help for non- or > >undersupported intrinsics like unsigned modulo, short multiplies, etc. > > > >If this were not done, then a user would just get mysterious > >"multiply-defined" errors from the link of his program. > > Well, it's *a* solution to a namespace problem, albeit one that > guarantees difficulties in mixing languages. Sure would have been > nice if they had put the underscores on the assembly-level routines > rather than mucking with the high level names. Is there a good > reason why it wasn't done this way? > There is a simple reason why this is done that I haven't seen posted yet (I may have missed it). And that is precisely to *preserve* existing names and make it *possible* to use common library routines in both Fortran and C. The problem of course is that Fortran requires that all parameters are passed by reference, with no provision for forcing passing by value (VMS Fortran has the %VAL extension, but there is nothing in standard Fortran77). Now, let us suppose you want to share routines between Fortran and C. You would very much like to be able to say "call foo(x)" in Fortran, and "foo(x);" in C, and have it give you the same thing. Unfortunately, the two are not equivalent: in Fortran, x is call by reference, in C it is call by value; Fortran normally passes real and double precision values in their 'natural' sizes , C normally always uses double precision. So either you have to make all C routines that might ever possibly be called from Fortran be call by reference, and all Fortran routines that call externals always use double precision (not a very good idea), or have separate but equal libraries for Fortran (also not so good), OR figure out some way to distinguish between the two, while presenting the illusion of uniformity to the average user. This last option is what is commonly done. By appending an underscore, you get a different name to play with, and to use to remap the interface. In the example, all you need do is write a tiny routine foo_, something like foo_(x) float *x; {foo( (double) (*x) );} (even this doesn't quite work, because in C (despite the declaration) x will probably still be treated like a double, so this would actually need to be written in asm to re-map the incompatibilities. But it serves to convey the idea). Now you can have only one version of foo that is callable from both C and Fortran. It seems to me to be a reasonable solution to an ugly problem. Is their a better one that someone knows about? -- Bron Campbell Nelson bron@sgi.com or possibly ..!ames!sgi!bron These statements are my own, not those of Silicon Graphics.