Usenet comp.unix.osf.misc (1992-2004)
 help / color / mirror / Atom feed
* C calling Fortran 77 shared library on OSF/1 (or DUnix ?)
@ 1996-05-02  4:00 Rachel Kay Street
  1996-05-02  4:00 ` @#$%!?!
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Rachel Kay Street @ 1996-05-02  4:00 UTC (permalink / raw)




Hello,

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 to OSF/1.  I have a few problems
(or questions) though.  Do you know any of the answers please ?

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 ?

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 ?

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 ?

Thanks for your 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 or 20437::street                *
****************************************************************



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: C calling Fortran 77 shared library on OSF/1 (or DUnix ?)
  1996-05-02  4:00 C calling Fortran 77 shared library on OSF/1 (or DUnix ?) Rachel Kay Street
@ 1996-05-02  4:00 ` @#$%!?!
  1996-05-04  4:00 ` Sergio Gelato
  1996-05-06  4:00 ` Steve Lionel
  2 siblings, 0 replies; 6+ messages in thread
From: @#$%!?! @ 1996-05-02  4:00 UTC (permalink / raw)



: 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 ?

My fortran is growing hazy, but I think that for character parameters,
you pass in both the address of the characters and the string length. For
an assumed size string, it uses that as the actual string length. Otherwise
it uses whatever the subprogram says the string length should be. The
string length could be packed with the characters or a separate parameter.
If you need assumed length strings, you're going to have to discover the
calling conventions.

I don't remember fortran strings as being variable length, that is with a
terminating mark or current string length. You're given a character vector
and ways to move it around, but not much more.

: 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 ?

Fortran common is the same as a c global variable.

	REAL X,Y,Z
	COMMON /XYZ/ X,Y,Z

is the same as 

	struct {float x,y,z;} xyz; /*or _xyz or xyz_ or _xyz_ or XYZ or ...*/

Fortran is case insensitive. Many compilers downcase the external names. Also
many compilers prefix or suffix a '_' to externals to prevent a collision with
library entry point. Many c and fortran compilers. So you may have to play 
games to get the underscores right.

The C version of block data is that you only define a global in one file.
In other source files, it must be declared extern. I don't know how ld 
decides which block data to load. Perhaps it only permits one among all
the *.o files?
-- 
The Queen who loves, the Queen of life,    | smryan@netcom.com  PO Box 1563
the Queen who straits, the Queen of strife;|          Cupertino, California
with gasp of death or gift of breath       | (xxx)xxx-xxxx            95015
she brings the choice of birth or knife.   |         I don't use no smileys



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: C calling Fortran 77 shared library on OSF/1 (or DUnix ?)
  1996-05-02  4:00 C calling Fortran 77 shared library on OSF/1 (or DUnix ?) Rachel Kay Street
  1996-05-02  4:00 ` @#$%!?!
@ 1996-05-04  4:00 ` Sergio Gelato
  1996-05-07  4:00   ` William Deich
  1996-05-06  4:00 ` Steve Lionel
  2 siblings, 1 reply; 6+ messages in thread
From: Sergio Gelato @ 1996-05-04  4:00 UTC (permalink / raw)



In article <4m9s2v$f6u@newton.cc.rl.ac.uk>,
Rachel Kay Street <R.K.Street@rl.ac.uk> wrote:
>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 ?

I don't know about DEC's Fortran for OSF/1, but that particular mix is
unlikely to work under other Unix'y implementations. The equivalent of
a Fortran CHARACTER array (CHARACTER*(N) array(length)) is usually something
like char array[length][N], or in some cases char array[length*N].
(On machines that are not byte-addressable, I would beware if N is
not a multiple of the word/byte size ratio.)
You then need to pass N as part of the argument list (if calling Fortran)
or retrieve it from the argument list (in a C routine called by Fortran).
The same N applies to all elements of the array.
If you look at the assembly code generated by the compiler for
      INTEGER FUNCTION L(S)
      CHARACTER*(*) S
      L=LEN(S)
      END
you should be able to figure out how the string length is being passed.
Reading the Fortran manual may be easier, if you have it.
Alternatively, you could try
#define N 10
char str[N];
int l_();
size_t ll = N;
printf("%d %d %d %d\n", l_(&ll, str), l_(str, &ll), l_(N, str), l_(str, N));
and study what it prints out (if it doesn't dump core, in which case you'll
have to try the possibilities separately).



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: C calling Fortran 77 shared library on OSF/1 (or DUnix ?)
  1996-05-02  4:00 C calling Fortran 77 shared library on OSF/1 (or DUnix ?) Rachel Kay Street
  1996-05-02  4:00 ` @#$%!?!
  1996-05-04  4:00 ` Sergio Gelato
@ 1996-05-06  4:00 ` Steve Lionel
  2 siblings, 0 replies; 6+ messages in thread
From: Steve Lionel @ 1996-05-06  4:00 UTC (permalink / raw)




In article <4m9s2v$f6u@newton.cc.rl.ac.uk>, street@wdcc1.bnsc.rl.ac.uk 
(Rachel Kay Street) writes:

|>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 ?

Because the DEC C compiler is physically included with the Digital UNIX
(OSF/1) operating system, and its use is licensed by the "Developers
Toolkit" option, the documentation is included on the same CD which holds
the operating system documentation.
|>
|>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 ?
|>
|>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 ?

Please refer to the "Mixed Language Programming" chapter of the DEC Fortran
User Manual (on the Layered Products Documentation Library CD) - it contains 
extensive information on mixing Fortran and C and should answer all of your 
questions.  In particular, it contains detailed examples of passing arrays and 
character arguments between Fortran and C.

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.

If you have further questions, feel free to send them to me by mail.
-- 

Steve Lionel                      mailto:lionel@quark.zko.dec.com
Fortran Development               http://www.digital.com/info/slionel.html
Digital Equipment Corporation     
110 Spit Brook Road, ZKO2-3/N30    
Nashua, NH 03062-2698             "Free advice is worth every cent"

For information on Digital Fortran 90, see http://www.digital.com/info/hpc/f90/



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: C calling Fortran 77 shared library on OSF/1 (or DUnix ?)
  1996-05-04  4:00 ` Sergio Gelato
@ 1996-05-07  4:00   ` William Deich
  1996-05-08  4:00     ` Sergio Gelato
  0 siblings, 1 reply; 6+ messages in thread
From: William Deich @ 1996-05-07  4:00 UTC (permalink / raw)



In article <4mg704$qbj@ictpsp10.ictp.trieste.it>,
Sergio Gelato <gelato@oort.ap.sissa.it> wrote:
>In article <4m9s2v$f6u@newton.cc.rl.ac.uk>,
>Rachel Kay Street <R.K.Street@rl.ac.uk> wrote:
>>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 ?
>
>I don't know about DEC's Fortran for OSF/1, but that particular mix is
>unlikely to work under other Unix'y implementations.

Actually, that mix will work under most flavors of Unix.  Among those
Unix's with which I am familiar, 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.

>The equivalent of
>a Fortran CHARACTER array (CHARACTER*(N) array(length)) is usually something
>like char array[length][N], or in some cases char array[length*N].
>(On machines that are not byte-addressable, I would beware if N is
>not a multiple of the word/byte size ratio.)

I am pretty sure (but I don't have a Fortran manual handy to check on this)
that the rules for Fortran EQUIVALENCE statements between character strings
and character arrays guarantees that this layout holds for all Fortran
implementations.  (That is, an array of character strings is laid out in
memory with one character string immediately following the previous.)

-Will

William Deich
UCO / Lick Observatory   |  Internet: will@ucolick.org
University of California |  Phone: (408) 459-3913



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: C calling Fortran 77 shared library on OSF/1 (or DUnix ?)
  1996-05-07  4:00   ` William Deich
@ 1996-05-08  4:00     ` Sergio Gelato
  0 siblings, 0 replies; 6+ messages in thread
From: Sergio Gelato @ 1996-05-08  4:00 UTC (permalink / raw)



In article <4mo2h2$cq1@darkstar.UCSC.EDU>,
William Deich <will@fast.ucolick.org> wrote:
>In article <4mg704$qbj@ictpsp10.ictp.trieste.it>,
>Sergio Gelato <gelato@oort.ap.sissa.it> wrote:
>>In article <4m9s2v$f6u@newton.cc.rl.ac.uk>,
>>Rachel Kay Street <R.K.Street@rl.ac.uk> wrote:
>>>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 ?

>>I don't know about DEC's Fortran for OSF/1, but that particular mix is
>>unlikely to work under other Unix'y implementations.

>Actually, that mix will work under most flavors of Unix.  Among those

No, it's the C side you don't seem to understand. We agree entirely on
the Fortran passing convention (the string length is passed as a separate
argument). I also agree that the equivalencing rules preclude such things
as padding to word boundaries between elements of the array (see below).
That means a CHARACTER*(n) array(l) is stored exactly like a
char array[l*n] in C. I would have to check the exact wording of the C
standard to see if char array[l][n] is guaranteed to be stored in the
same way (I already know that it will be in the implementations I use).

The essential point here is that char *array[], which is indeed passed to
subprograms as if it were char **array, is a different animal: an array
of pointers to character variables. These variables are not guaranteed to
have the same length, nor to be contiguous in memory, nor to be
non-overlapping, and some of the pointers may even be NULL.
Therefore, a char ** argument in C is generally not compatible with a
Fortran array of character strings.

>Unix's with which I am familiar, 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.

>>The equivalent of
>>a Fortran CHARACTER array (CHARACTER*(N) array(length)) is usually something
>>like char array[length][N], or in some cases char array[length*N].
>>(On machines that are not byte-addressable, I would beware if N is
>>not a multiple of the word/byte size ratio.)

>I am pretty sure (but I don't have a Fortran manual handy to check on this)
>that the rules for Fortran EQUIVALENCE statements between character strings
>and character arrays guarantees that this layout holds for all Fortran
>implementations.  (That is, an array of character strings is laid out in
>memory with one character string immediately following the previous.)

That certainly needs to be guaranteed in the presence of EQUIVALENCE
statements, actual or potential (and all COMMON blocks imply the possibility
of such equivalencing). It would be cumbersome for the compiler to generate
different code for a routine according to whether the actual argument is
equivalenced or not, so we can probably rely on most implementations never
introducing any padding between array elements.



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~1996-05-08  4:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1996-05-02  4:00 C calling Fortran 77 shared library on OSF/1 (or DUnix ?) Rachel Kay Street
1996-05-02  4:00 ` @#$%!?!
1996-05-04  4:00 ` Sergio Gelato
1996-05-07  4:00   ` William Deich
1996-05-08  4:00     ` Sergio Gelato
1996-05-06  4:00 ` Steve Lionel

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