Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cmcl2!brl-adm!brl-smoke!gwyn From: gwyn@brl-smoke.ARPA (Doug Gwyn ) Newsgroups: comp.lang.c Subject: Re: compilers and linkers Message-ID: <6674@brl-smoke.ARPA> Date: Sat, 14-Nov-87 15:41:47 EST Article-I.D.: brl-smok.6674 Posted: Sat Nov 14 15:41:47 1987 Date-Received: Sun, 15-Nov-87 18:41:29 EST References: <2522@calmasd.GE.COM> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 36 In article <2522@calmasd.GE.COM> pal@calmasd.GE.COM (Peter Lawrence) writes: > It appears that in Unix derived C compilers global names get an >underscore prepended to them before they end up as symbols in the object file. >It also appears that in Unix derived Fortran compilers global names get an >underscore appended to them. I am sure there is going to be a bad reason for >this but I have to know: why the underscores and why in different places. >Most other compilers dont do this kind of thing, but how in Unix environments >does one link C and or Fortran with Pascal for example. Not all UNIX-derived C compilers prepend an underscore to globals, but many do. The reason for this should be obvious by considering: double R0; void iterate(double r) { R0 = r * R0 * (1 - R0); } What assembly code would this generate? If the assembler knows "R0" as the name of register #0, the appearance of the global names unmodified as operands in the assembly language could be confused with the registers and the code would be broken. Some assemblers do not have this problem, and for them the C compiler normally would not prepend an underscore. Fortran subprograms are given different names from C functions because the two languages do not have the same linkage conventions (calling sequences), so to prevent C library functions from intruding in Fortran's external name space, the postpended-_ convention ensures that they are not meaningful names to Fortran. (Note that Fortran globals normally also have prepended underscores in evironments where that is done for C globals, for the same reasons.) It is fairly easy for most UNIX Fortrans to write a C function that can be called from Fortran, once the Fortran linkage conventions are understood. The C function name would of course be the same as its Fortran counterpart (probably mapped to lower-case) with an underscore appended. Chris Torek periodically posts an article on C/Fortran interfacing, the last posting being not very long ago as I recall. You should also be able to find information about this in your Programmer's Guide.