Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Optional argument link error

Martin__Paul
New Contributor I
1,318 Views

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
 END

[/fortran]
It works fine without the optional parameter. Why can the linker not resolve this?

Thanks.

0 Kudos
5 Replies
Steven_L_Intel1
Employee
1,318 Views
Given that the suffix is @12, the calling procedure thinks that you didn't add the extra argument, or that the datatype of the first argument is not CHARACTER. My guess is that the problem is in a part of the code you didn't show us.

Why not put APICALL in a module and USE it? That eliminates the chance of error with a separate interface.
0 Kudos
Martin__Paul
New Contributor I
1,318 Views

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.

0 Kudos
Steven_L_Intel1
Employee
1,318 Views
It would help if you could attach a small but complete test program that demonstrates the problem. Which compiler are you using? I note that you're using STDCALL, so either you're using CVF or you have the /iface:cvf option set in Intel Fortran.
0 Kudos
Martin__Paul
New Contributor I
1,318 Views
I'm using 11.1.060; both sub routines are in the same fortran project in separate source files.
0 Kudos
Steven_L_Intel1
Employee
1,318 Views
Please attach a test case so I can look at it.
0 Kudos
Reply