- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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:
Method 2:
Method 1:Project/Properties/Fortran/External routines/Calling convention: default implies
Jugoslav
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*)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,
...
SUBLOAD(facilityID)
...
subroutine SUBLOAD(facilityID)
!DEC$ATTRIBUTES REFERENCE:: facilityID
character(30) facilityID
Method 1:Project/Properties/Fortran/External routines/Calling convention: default implies
#define FORTRAN_CALL __cdeclMethod 2: Project/Properties/Fortran/External routines/Calling convention: CVF implies
#define FORTRAN_CALL __stdcallIt seems you already have method 2 working, since linking goes OK for non-character strings.
Jugoslav
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.

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