Usenet comp.unix.osf.misc (1992-2004)
 help / color / mirror / Atom feed
From: street@wdcc1.bnsc.rl.ac.uk (Rachel Kay Street)
Subject: Answers to C calling Fortran 77 shared library on OSF/1 (long)
Date: 1996/05/22	[thread overview]
Message-ID: <4nutvs$8pu@newton.cc.rl.ac.uk> (raw)




Hello again.  I wrote on 2 May 1996 08:34:39 GMT with some questions.
Thanks to all those who responded, especially the following:-

  Dan Riley <dsr@lns598.lns.cornell.edu>, 
  David Metsky <metsky@zk3.dec.com>,
  William Deich <will@fast.ucolick.org>, and
  Steve Lionel <lionel@quark.zko.dec.com>.

Here are the answers to the questions (answers taken mostly from people's
email to me) :-

>
>I am sorry if you have seen these two sets of questions before
>but I am not getting answers and wonder if there are no answers
>or my message never reached the net.
>
>I am working on a large shared library written in a mixture of
>C and Fortran being ported from VAX/VMS 5.4 to OSF/1 V3.2.  I have
>a few problems or questions) though.
>Do you know any of the answers please ?
>
General) 
Fortran compilers under BSD 4.x; SunOS, Solaris, OSF/1, AIX, and 
HP-UX, all support the BSD f77 convention for passing storing and
passing [arrays of] character strings.  (HP-UX and AIX have compile-time
switches that let you select between BSD's f77 string-passing convention
and another convention.)

The BSD f77 rule is that a pointer to the start of the data is passed
in the argument's normal position in the calling list, and the length
of a character string is attached as an extra argument at the end of
the argument list.

See http://ra.stsci.edu/documents/UUG/UnixGuide.book_59.html for an
introduction.

>
>1) Does anyone know if DEC C manual set for OSF/1 exists as I cannot
>   find any bookreader files yet ?  If they exist which CD are they on,
>   for which OS versions and where ?
>
A1)
There's a DEC C Language Reference Manual that ships with V4.0 (disk as yet
unknown).  Basic information on C available from AXP/VMS manuals for now.
Also see the "Mixed Language Programming" chapter of the current DEC Fortran
User Manual (on the Layered Products Documentation Library CD) - it contains 
some information on mixing Fortran and C and should answer basic questions.
In particular, it contains detailed examples of passing arrays and character 
arguments between Fortran and C.

>
>2) Also any one tried to mix character*(*) array(length) and char *array[]
>   (or char **array) when making calls from C into Fortran 77 functions ?
>   Do you have an example I could read as all I can do is create core ?
>   I have managed to pass single character strings by blank filling and
>   null terminating single string in C, with Fortran index searching for
>   char(0).  Is there a better method as without explicit limits say a
>   Fortran print of the string just keeps on printing out memory until
>   it dumps core ?
>
A2)
Two things you need to know:

1) when fortran passes a character string, it adds an extra int argument
   to the end of the arg list with the length of the string (note, this
   is really an int, not an int*).

2) When fortran is passed character*(*) array(length), it assumes that
   the array is in a contiguous block of memory, with each element the
   length indicated in the extra argument.  This means your char**
   must be allocated as one block and then divided up into equal length
   strings.

The following simple example demonstrates:

      call fee()
      end
      subroutine foo(s, j)
      character*(*) s(*)
      do 10 i = 1, j
          write(6,*) s(i)
 10   continue
      end
 
#include <stdio.h>

char s[][10] = {
  "one",
  "two",
  "three",
  "four"
};

extern void foo_(char*, int*, int);

void fee_()
{
  int i, j = 4;

  foo_(s[0], &j, 10);
}

PS " Passing constants from C to Fortran is basically impossible
since you can't extract an address to pass (unlike in VAX VMS C,
where the compiler will accept &1). The one exception is character
strings, since strings are really pointers anyway. "

>
>3) What do I do with the common blocks when I create the shared library
>   from its object and 'normal' library parts?  Does ld know how to find
>   the commons and deal with them automatically ?  What about block data
>   units ones ?
>
A3)
ld seems to do the right thing with commons as long as the common is
declared the same length everywhere.  If the common has a longer
length in the calling program than it does in the shared library,
trouble ensues--the length in the shared library seems to take
precedence regardless.

ld does know how to find the COMMONs, but of course there should be only one
instance of a BLOCK DATA program unit and this should be in the shared library.
 
---

Thanks again for your help and time.

Rachel Street

-- 
****************************************************************
* R K Street - RAL Database Systems - (01235) 821900 ext 5833  *
* Rutherford Appleton Laboratory, Chilton, DIDCOT, Oxon, UK    *
* Email: TCPIP  street@wdcc1.bnsc.rl.ac.uk                     *
*            or R.K.Street@rl.ac.uk                            *
*        DECNET wdcc1b::street                                 *
****************************************************************



-- 
****************************************************************
* R K Street - RAL Database Systems - (01235) 821900 ext 5833  *
* Rutherford Appleton Laboratory, Chilton, DIDCOT, Oxon, UK    *
* Email: TCPIP  street@wdcc1.bnsc.rl.ac.uk                     *
*            or R.K.Street@rl.ac.uk                            *
*        DECNET wdcc1b::street                                 *
****************************************************************



             reply	other threads:[~1996-05-22  4:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-05-22  4:00 Rachel Kay Street [this message]
1996-05-25  4:00 ` Answers to C calling Fortran 77 shared library on OSF/1 (long) Mike Shannon
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox