Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29298 Discussions

Call Fortran function in a module from C++?

AONym
New Contributor II
2,011 Views

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++?

0 Kudos
1 Solution
mecej4
Honored Contributor III
1,975 Views

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();
}

View solution in original post

4 Replies
andrew_4619
Honored Contributor III
1,992 Views

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.

0 Kudos
mecej4
Honored Contributor III
1,976 Views

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();
}
AONym
New Contributor II
1,849 Views

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.

0 Kudos
mecej4
Honored Contributor III
1,828 Views

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.

0 Kudos
Reply