Software Archive
Read-only legacy content
17061 Discussions

Passing Character Strings from C to Fortran (Again!)

jcbreeding
Beginner
271 Views
I read Jugoslav Dujic's reply to this same question but I am still confused about how to pass a character string from C to Fortran. The following program produces the linker error:
cstuff.obj : error LNK2001: unresolved external symbol _PRINTSTUFF@8

What am I doing wrong? I know it has something to with the hidden length arg passed with the character string but I am apparently having a senior moment and can't get past this problem.

Fortran code (tstcall.f)

      PROGRAM TSTCALL  
         INTERFACE  
            SUBROUTINE oops  
            !DEC$ATTRIBUTES C :: oops  
           END SUBROUTINE oops  
        END INTERFACE  
  
       CALL oops  
       END  
  
       SUBROUTINE PRINTSTUFF(CSTRING,NCHR)  
          INTEGER NCHR  
         CHARACTER*(*) CSTRING  
        WRITE(*,'(A)') CSTRING(1:NCHR)  
        RETURN  
        END  


C code (cstuff.c)

#include   
#include   
  
extern void _stdcall PRINTSTUFF(char* cstring, int nchr);  
void oops()  
{   char cstring[20];  
    int nchr;  
    strcpy(cstring,"This is a test!");  
    nchr = strlen(cstring);  
    PRINTSTUFF(cstring,nchr);  
}
0 Kudos
1 Reply
Jugoslav_Dujic
Valued Contributor II
271 Views
PRINTSTUFF in your code will be exported as _PRINTSTUFF@12 (LOC(Cstring) plus hidden LEN(CString) plus NCHR) thus linker complains. Remove NCHR argument from Fortran's arg-list -- C will put nchr onto stack, and Fortran will "know" the exact length of CString via hidden length argument, i.e WRITE(*,"(A)") Cstring (without (1:NCHR)!) will yield desired result.

Jugoslav
0 Kudos
Reply