Calling C from FORTRAN under Unix

Rob Gabbard wggabb at sdrc.COM
Thu May 23 04:44:20 AEST 1991


>From article <5343 at dftsrv.gsfc.nasa.gov>, by packer at amarna.gsfc.nasa.gov (Charles Packer):
> On a Unix system I want to call a C routine from a FORTRAN
> program. The linker says the C routine is undefined.  On a VMS
> system, the problem doesn't happen. The reason why there is a
> problem on a Unix system has something to do with the name of
> the C subroutine acquiring a preceding underscore ("_") during
> compilation, or some such nonsense. To anyone familiar with this
> problem: what is the way around it? 

Try declaring your C routine as routinename_
We get around this by having a host dependent include file with a 
special macro in it used in our C function definitions like this:

In the include file (e.g. portable.h):

For an "underscore" machine....
#define IDENTITY(x)		x
#define FORTRAN_CALLABLE(x)	void IDENTITY(x)_

For a "non-underscore" machine....
#define FORTRAN_CALLABLE(x)	void x

Example C funtion definition:

#include "/wherever.../portable.h"

FORTRAN_CALLABLE(routine)(int foo, char *fubar)

The IDENTITY macro is a kludge to get around the fact that x_
doesn't work. The void is there to keep our programmers from designing
any C functions that return values like Fortran functions.  Since there
is no defined standard for this we don't allow it.  In all systems I've
seen returning an int works but you never know.

-- 
The statements above are my own and do not neccesarily reflect the opinion of 
my employer.
-------------------------------------------------------------------------------
Rob Gabbard 				    wggabb at sdrc.sdrc.com
Technical Development Engineer
Structural Dynamics Research Corporation



More information about the Comp.unix.questions mailing list