- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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)
C code (cstuff.c)
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); }
Link Copied
1 Reply
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Jugoslav
WRITE(*,"(A)") Cstring
(without (1:NCHR)!) will yield desired result. Jugoslav
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page