[comp.lang.fortran] Mixed linking

john@riddle.UUCP (Jonathan Leffler) (07/08/88)

In article <3670@briar.Philips.Com> sxm@bebop.philips.com (Sandeep Mehta) writes:
>I cannot get to pass character strings from C to routines FORTRAN correctly
>(under UNIX) ?

If you have or can get hold of a Version 7 Unix Programmer's
Manual Volume 2, the article on ``A Portable Fortran 77
Compiler'' describes the interface between C and Fortran.

Quickly, when character strings are passed to Fortran, you have
to pass an extra argument, which is a long(!).  This extra
argument goes at the end of the list.  If there are several
strings, there is a length for each argument in left-right order.

Thus, if the FORTRAN would look like:

CALL fungus("abcdef", "xyzpqrzzz")

the C call would look like:

_fungus("abcdef", "xyzpqrzzz", 6L, 9L);

Try mailing me if you need more help.

Jonathan Leffler (john@sphinx.co.uk)	#include <disclaimer.h>

mikel@pyrps5 (Mike Lipsie) (07/10/88)

In article <604@riddle.UUCP> john@sphinx.co.uk (Jonathan Leffler) writes:
>In article <3670@briar.Philips.Com> sxm@bebop.philips.com (Sandeep Mehta) writes:
>>I cannot get to pass character strings from C to routines FORTRAN correctly
>>(under UNIX) ?
>
>If you have or can get hold of a Version 7 Unix Programmer's
>Manual Volume 2, the article on ``A Portable Fortran 77
>Compiler'' describes the interface between C and Fortran.

Additionally, it is important to remember that C and FORTRAN strings are
very different beasts.  A C string is a piece of memory terminated by
a null character while a FORTRAN string is a fixed piece of memory
which may or may not contain null characters.  Thus, in the example,
the two strings which look identical would have different actual
representations.

The example that Jonathan included (that I omitted for space reasons)
is valid only for one particular implementation (the standard VAX
UNIX compilers).  In general, interlanguage calling sequences are
not portable.
-----------
Mike Lipsie  {allegra,cmcl2,decwrl,hplabs,ut-sally,utzoo}!pyramid!mikel
Pyramid Technology Corp, Mountain View, CA  +1 415 965 7200 ext. 4980