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

Link error in C++ project that links to Fortran library

andrew_terekhov
Beginner
652 Views
I have a C++ program that links to a static Fortran library. I use VC 7.1 for C++ and IVF for Fortran projects. I get a link error LNK2019: unresolved external symbol _SUBLOAD@4 referenced in function _DASUBLOAD. It happens if the SUBLOAD Fortran subroutine has a parameter of type character array:

subroutine SUBLOAD(facilityID)
character facilityID(30)
end subroutine

If I change the parameter type to something else I don't get the link error. I created the simplest test case to demonstrate the problem and attached the source and project files.
0 Kudos
2 Replies
Jugoslav_Dujic
Valued Contributor II
652 Views
You have two problems:

1) Passing character strings requires special attention; normal Fortran mechanism is to pass a char* and an int argument, the latter being string length. In purely Fortran sources, the latter is not explicated in code, but the compiler generates it beyond the scene. You can suppress that by inserting !DEC$ATTRIBUTES REFERENCE attribute though, but in that case you have to declare string length explicitly in the routine. So:

Method 1:
extern "C" void FORTRAN_CALL SUBLOAD(char*, int)
...
SUBLOAD(facilityID, strlen(facilityID))
...
subroutine SUBLOAD(facilityID)
character(*) facilityID

Method 2:
extern "C" void FORTRAN_CALL SUBLOAD(char*)
...
SUBLOAD(facilityID)
...
subroutine SUBLOAD(facilityID)
!DEC$ATTRIBUTES REFERENCE:: facilityID
character(30) facilityID
2) The other problem can be related with the calling convention you select in IVF (FORTRAN_CALL symbol above). IVF default is __cdecl, but you seem to have used __stdcall (CVF one). Both are OK as long as you're consistently defining it. So,
Method 1:Project/Properties/Fortran/External routines/Calling convention: default implies
#define FORTRAN_CALL __cdecl
Method 2: Project/Properties/Fortran/External routines/Calling convention: CVF implies
#define FORTRAN_CALL __stdcall
It seems you already have method 2 working, since linking goes OK for non-character strings.

Jugoslav
0 Kudos
andrew_terekhov
Beginner
652 Views
Jugoslav, thanks a lot for your fast reply. After I changed call interface to __cdecl (default in Fortran) my problem went away.

Now I have another question: I need to call a C function from Fortran code. How do I do this in IVF? I use IVF version 8.
0 Kudos
Reply