[comp.lang.fortran] How to call a fortran subroutine in a C program?

Mingqiu Sun <sun@pacific.mps.ohio-state.edu> (03/26/91)

This question is probably asked before. Is there an easy way to call
a subroutine written in fortran by a C program?  I am talking about
something like commercial IMSL subroutines, which I have no access of
the original fortran source codes. I need only to know how to do it
in a UNIX environment.

Thanks.

corrigan@weber.ucsd.edu (Michael J. Corrigan) (03/27/91)

In article <2773@pacific.mps.ohio-state.edu> sun@pacific.mps.ohio-state.edu (Mingqiu Sun) writes:
>
>This question is probably asked before. Is there an easy way to call
>a subroutine written in fortran by a C program?  I am talking about
...

Some of the principles involved are :
1. You must pass only pointers from C to the fortran, since as you know fortran
expects to be able to change the value of a variable when it is passed to a 
subroutine
Principle is C is call-by-value, Fortran is call-by-reference
2. In some cases the order of the arguments must be reversed,  between the
caller and the callee, sometimes not ( implementation dependent)
(easy to check)
Principle is the order in which the arguments to a function are pulled
of the stack is compiler dependent ( and not required to be consistent
between languages/compilers since such things are "internal", I suppose)
Some of the other facts/tricks are:
3. in some cases you must name the fortran subroutine sub_ in the C call
since the original fortran compiler that compiled the, say IMSL, libraries
would have added that character to the name that you would think it is
(really easy to check - the linker says can't find aaa_)
4. Sometimes you can't easily make the call directly but must call 
a helper/wrapper routine in one language or the other to straighten things
out ( at least if you wish to preserve some appearance of semantics)

Feel free to email me if you can't make it work or want an example.

Michael J. Corrigan
corrigan@ucsd.edu