Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 (MU) 9/23/84; site munnari.OZ Path: utzoo!linus!decvax!mulga!munnari!lizs From: lizs@munnari.OZ (Liz Sonenberg) Newsgroups: net.lang.f77,net.lang.c Subject: Re: calling c routines from fortran Message-ID: <595@munnari.OZ> Date: Fri, 23-Nov-84 19:09:46 EST Article-I.D.: munnari.595 Posted: Fri Nov 23 19:09:46 1984 Date-Received: Sat, 24-Nov-84 08:36:13 EST References: <100@athena.UUCP> <1265@hao.UUCP> Organization: Comp Sci, Melbourne Uni, Australia Lines: 48 > Unfortunately, due to the fact that F77 insists on appending a "_" to all > of it's routine names, you cannot call *any* C routine from F77 unless it's > name ends in "_". In addition to that, all F77 arguments are pointers, so you > can't even call a routine whose name ends in "_" if it is expecting any non- > pointer arguments (usually the case in C). Therefore, the only way to do this > is the following (which is what I gather Liz was suggesting) >[ details of proposed method comes here ] Actually there is a rather horrible kludge for passing an immediate value (rather than a pointer) directly from an f77 program to a c procedure. Suppose you want to call the c procedure whose synopsis is int z_(intvar) int intvar; The following f77 code will pass the value of the integer quantity i to the procedure z_ as an immediate value ... character*1 x(2) ... iretval = z( x( i + 1 - loc(x) ) ... (provided your machine is byte-addressable, and each character occupies a single byte). Naturally, you would have to compile the resulting program without the -C option so that no run-time checking of array subscripts will be performed. Getting access to immediate values that are passed back the other way is a different story, however, and I know of no convenient way of doing it. It is also possible to circumvent the compiler's (very irritating) procedure of appending underscores to all user-established global variables, provided the name of the c routine you want to call contains at least two characters. When calling the routine from your f77 program, give it any convenient name that contains one less character. After compiling (but not linking) the f77 progam, the temporary name you have given the routine will appear in the string table of the object file with an underscore appended. It is not particularly difficult to replace this with the true name of the routine you want to call ( I have a rudimentary c program for doing this, if anyone is interested ). After the modified object file has been linked with the appropriate c files or libraries, your f77 program will then call the correct routine. David Wilson { ...!decvax!munnari!lizs }