Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!rutgers!umd5!mimsy!rab From: rab@mimsy.UUCP (Bob Bruce) Newsgroups: comp.lang.c Subject: Re: compilers and linkers Message-ID: <9361@mimsy.UUCP> Date: Sat, 14-Nov-87 11:20:47 EST Article-I.D.: mimsy.9361 Posted: Sat Nov 14 11:20:47 1987 Date-Received: Sun, 15-Nov-87 19:47:43 EST References: <2522@calmasd.GE.COM> Reply-To: rab@mimsy.UUCP (Bob Bruce) Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 43 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 ... There are several reasons for this. Most Unix compilers produce assembly code at some point during compilation. Imagine what the assembler will think if you use symbols such as `sp', `r0', `push', `pop', `add', `and', etc. Since these symbols may have some type of special meaning to the assembler, such as register names or operand mnemonics, they cannot be used as variable names. If you pass symbols directly to the assembler then you must restrict the name space available to C programmers. By prepending an underscore this problem is avoided. Another advantage of prepending the underscore is that when you write assembly language routines, as long as you don't prepend underscores you can create global names that are unaccessible from C code. This makes for more modular code. This is useful if you want to write optimized assembly routines that don't use the same procedure call protocol as C. (e.g: The Sun C compiler uses this technique to call `ldivt', `lmult', etc. to emulate 32 bit arithmetic when generating 010 executables.) > ... and why in different places. The C and fortran put the underscores in different places so that you can have the same functions with the same name available in each (e.g., sin, cos, etc), and still be able to link C and fortran code with each function being extracted from the appropriate library. (i.e. _sin from the C math library, _sin_ from the fortran library). >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. If you want to reference a fortran variable in C then you prepend an underscore to the variable in your C program. So `INTEGER X' in fortran is `int X_' in C. (There is, of course, no guarantee that an `int' in C is the same size, or referenced in the same manner, as an `INTEGER' in fortran.)