This is the mail archive of the mailing list for the Cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

C calls to Fortran with strings


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 <stdio.h>

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

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

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

      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'

------- 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:
   1036 [main] test 1000 stackdump: Dumping stack trace to

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

Want to unsubscribe from this list?
Send a message to

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]