delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/01/12:46:57

From: martin DOT kahlert AT keksy DOT mchp DOT siemens DOT de (Martin Kahlert)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Calling fortran subrotuines from C
Date: 1 Oct 1997 06:48:34 GMT
Organization: Siemens Inc.
Lines: 42
Message-ID: <60sro2$rik$1@salomon.mchp.siemens.de>
References: <343147a1 DOT 399544 AT news DOT uni-siegen DOT de>
Reply-To: Martin DOT Kahlert AT mchp DOT siemens DOT de
NNTP-Posting-Host: 139.23.187.70
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

[Posted and mailed]

In article <343147a1 DOT 399544 AT news DOT uni-siegen DOT de>,
	kretlow AT hrz DOT uni-siegen DOT d400 DOT de (Mike Kretlow) writes:
> Hi,
> 
> possibly my question is a faq, but I didn't found any hint in my docs
> and it is urgent.
> 
> I have some f77 subroutines, which I like to call from my c program.
> I compiled the file sub.f with g77 under dos and the main c-program
> with djgpp 2, but the linker couldn't find the subroutines (I declared
> them of course as extern).

There are some things to do:

First a FORTRAN subroutine FOO has to be declared and called by C as
extern void foo_;
foo_(parameters);
(g77-Fortran appends underscores to all subroutine-names)

Second: All parameters are called by reference, i.e.:

SUBROUTINE FOO(A,B,C)
INTEGER*4 A,
REAL*4 B(10)  <= Has values B(1),...,B(10)
REAL*8 C(3,4) <= In memory as C(1,1),C(2,1),C(3,1),C(1,2),...,C(3,4)

Has to be called from C:
int a;
float b[10];   <= Fill in values b[0],...,b[9]
double c[4,3]; <= In FORTRAN the leftmost index runs fastest, 
                  so change index-order.
foo_(&a,b,c);

Third: Try to avoid passing character strings to Fortran.
(They are passed by reference, too, but compiler-specific length
 information is appended to the parameter list as a long value 
 at the end of all parameters - there is no 0-termination...)

Hope that helps,
Martin.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019