- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I want to add an OPTIONAL parameter to a sub, but the linker cannot find the modified function resulting in error LNK2001: unresolved external symbol _MYSUB@12. I have declared an interface for the function as follows in the calling sub:
[fortran]SUBROUTINE MYSUB (APICALL, RETVAL, PROG_ID) CHARACTER*(*) APICALL INTEGER :: RETVAL INTEGER, OPTIONAL :: PROG_ID END[/fortran]
The body of the sub is thus (in separate source file):
[fortran]SUBROUTINE MYSUB(APICALL, RETVAL, PROG_ID) IMPLICIT NONE INTEGER :: RETVAL, L_UNIT CHARACTER*(*) APICALL INTEGER, OPTIONAL :: PROG_ID CALL EMPTYUNIT(L_UNIT) OPEN(L_UNIT, FILE = 'OUTFILE.LOG') IF (PRESENT(PROG_ID)) THEN WRITE(L_UNIT, *) APICALL, ' val=', RETVAL, 'fID=', PROG_ID ELSE WRITE(L_UNIT, *) APICALL, ' val=', RETVAL ENDIF CLOSE (L_UNIT) RETURN ENDIt works fine without the optional parameter. Why can the linker not resolve this?
[/fortran]
Thanks.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Why not put APICALL in a module and USE it? That eliminates the chance of error with a separate interface.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks, I have checked that the datatypes match up.
Local declarations in the caller:
(interface as before)
INTEGER :: retVal = 0
INTEGER PROG_ID (dummy arg)
The sub is then called as follows:
CALL MYSUB('Test', retVal)
-OR-
CALL MYSUB('Test', retVal, PROG_ID)
MYSUB defined in separate source file as before.
If I declare string APICALL in a module instead of passing it as a parameter the same link issue appears (except with unresolved external symbol _MYSUB@4, i.e. it always expects the optional parameter, so the cases where I omit the optional int and pass a single int argument now won't link.) I can't fathom why the linker can't resolve this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page