- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have a Fortran function returning an integer which I want to call from C++. Here is a simplified version of it.
MODULE Model
INTEGER :: error
CONTAINS
INTEGER FUNCTION CloseFiles() &
BIND(C, name='CloseFiles') RESULT(nClosed)
USE, INTRINSIC :: ISO_C_BINDING
IMPLICIT NONE
LOGICAL :: isOpen
nClosed = 0
RETURN
END FUNCTION CloseFiles
END MODULE Model
Here is how I am trying to access it from C++:
extern "C" int MODEL_mp_ERROR;
extern "C" int MODEL_mp_CloseFiles(void);
int nClosed = MODEL_mp_CloseFiles();
When I link the C++ project, I get
error LNK2019: unresolved external symbol MODEL_mp_CLOSEFILES referenced in function ...
I've tried with 'CloseFiles' and 'CLOSEFILES' in both places.
Dumpbin on the Fortran static library does not show any symbol containing 'CloseFiles', either upper-, lower- or mixed-case.
Variables in the module Model can be accessed; for instance, the Fortran variable 'error' does not get a linker error.
How can I call the Fortran integer function, which is in a Fortran module, from C++?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
When you specify BIND(C,...), the Fortran decoration of <module_name>_mp_ is not applied. The OBJ from the following C program will link with the OBJ from your Fortran source file.
extern int CloseFiles(void);
void main(void){
int i;
i=CloseFiles();
}
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You mention static library so I guess you made a lib. However the linker will also need to be able to see the Fortran mod file as well as the lib. I am also not 100% convinced that the decoration MODEL_mp_ is required.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
When you specify BIND(C,...), the Fortran decoration of <module_name>_mp_ is not applied. The OBJ from the following C program will link with the OBJ from your Fortran source file.
extern int CloseFiles(void);
void main(void){
int i;
i=CloseFiles();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This did the job.
I wonder why it is possible to make a function "local" for Fortran, in the sense that it isn't available unless you USE the module, but global for C++. In this respect, functions apparently differ from variables. when they both are in a Fortran module.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Unlike C/C++, Fortran 77 and earlier versions had no global variables, no function/subroutine prototypes, and provided implicit typing of variables.
Later versions of Fortran, namely, Fortran 90, etc., were created with great emphasis on keeping old Fortran code working without needing changes.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page