C calls to Fortran with strings

tprinceusa@mindspring.com tprinceusa@mindspring.com
Mon Nov 20 07:58:00 GMT 2000


erik.nim@risoe.dk wrote:
> Hi,

I am using the gcc compiler under Cygwin on a NT platform for applications
where I call Fortran subroutines from C code. This is working smoothly with
one strange exception: If more than one string are present in Fortran
subroutines the program crashes. Apparently everything works with ONE string
only. The following simple example illustrates the problem: 

-------- main.c ----------

#include 

extern void sub1_(char *s1, unsigned int);
extern void sub2_(char *s1, unsigned int, char *s2, unsigned int);

int main() {

  static char s1[] = "String1 ver 0";
  static char s2[] = "String2 ver 0";

  /*  This works well  */
  printf("\n First test:\n");
  printf(" In main: s1 = %s. Length = %d\n", s1, strlen(s1));
  sub1_(s1, strlen(s1));
  printf(" In main: s1 = %s. Length = %d\n", s1, strlen(s1));

  /*  This crashes  */
  printf("\n Second test:\n");
  sub2_(s1, strlen(s1), s2, strlen(s2));
  printf(" In main: s1 = %s. Length = %d\n", s1, strlen(s1));
  printf(" In main: s2 = %s. Length = %d\n", s2, strlen(s2));

  return(0); }

-------- sub1.f ------- 

      SUBROUTINE SUB1(S1)
*
      CHARACTER*(*) S1
*
      WRITE(*, *) 'In SUB1: S1 = ', S1, '. Length = ', LEN(S1)
*
      S1 = 'String1 ver 1'
      RETURN
      END

------- sub2.f ------- 

      SUBROUTINE SUB2(S1, S2)
*
      CHARACTER*(*) S1, S2
*
      WRITE(*, *) 'In SUB2: S1 = ', S1, '. Length = ', LEN(S1)
      WRITE(*, *) 'In SUB2: S2 = ', S2, '. Length = ', LEN(S2)
*
      S1 = 'String 1 ver 2'
      S2 = 'String 2 ver 2'
*
      RETURN
      END

------- makefile ------- 

test: main.o sub1.o sub2.o
	gcc -Wall -o test main.o sub1.o sub2.o -lg2c

main.o: main.c
	gcc -Wall -c main.c

sub1.o: sub1.f
	gcc -Wall -c sub1.f

sub2.o: sub2.f
	gcc -Wall -c sub2.f

------- output ------- 

 First test:
 In main: s1 = String1 ver 0. Length = 13
 In SUB1: S1 = String1 ver 0. Length =  13
 In main: s1 = String1 ver 1. Length = 13

 Second test:
 In SUB2: S1 =
      0 [main] test 1000 handle_exceptions: Exception:
STATUS_ACCESS_VIOLATION
   1036 [main] test 1000 stackdump: Dumping stack trace to
test.exe.stackdump
-----------------

Can any one tell me what's wrong and how I possible can avoid the problem? 


Kind regards 


Erik Nim

Risoe National Laboratory
Wind Energy and Atmospheric Physics Department
Denmark

IIRC, the calling convention of f2c/g77 places all the string length arguments at the end of the calling sequence.  You could verify this most easily with an f2c translation of your Fortran.  Other helpers which have been recommended include "cfortran.h" and similar tools which you should find discussed in the archives of comp.lang.fortran.  F2K intends to standardize this, but up to now, f2c/g77 are the closest to a standard.

HTH

Tim

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com



More information about the Cygwin mailing list