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

Calling Fortran routines contained in modules from C

carlls
Beginner
701 Views
Well it seems it is daily post time..... I need to call a FORTRAN subroutine that is defined in a module (as defined below) from a C/C++ calling routine.

Given a F90 source that looks like:

Module MyModule_API

CONTAINS

subroutine my_sub()
print *, 'Hi Dad, Send money!'
stop
end subroutine my_sub

End Module

I create a obj file. When I write my C code as

int call_f()
{
my_sub();
}

I get unresolved externalls, so being resourceful I run dumpbin /symbols on the OBJ and see that FORTRAN has mangled the name to MyModule_API_mp_my_sub ... so changing my code to :

int call_f()
{
MyModule_API_mp_my_sub();
}

Works, which is good but rather tedious.

Given this data I could write MyModule.h for my C code that has

#define my_sub MyModule_API_mp_my_sub

void MyModule_API_mp_my_sub(void);

But this seems unruly ... is there a better way?

Regards
Carl
0 Kudos
6 Replies
TimP
Honored Contributor III
701 Views
Quoting - carlls

Module MyModule_API

CONTAINS

subroutine my_sub()
print *, 'Hi Dad, Send money!'
stop
end subroutine my_sub

End Module

I could write MyModule.h for my C code that has

#define my_sub MyModule_API_mp_my_sub

void MyModule_API_mp_my_sub(void);

But this seems unruly ... is there a better way?

Such as subroutine my_sub() bind(c) ? (look up use iso_c_binding)
0 Kudos
jimdempseyatthecove
Honored Contributor III
701 Views
Quoting - tim18
Such as subroutine my_sub() bind(c) ? (look up use iso_c_binding)

Aproblem with this is the module may name the subroutine in a common manner and therefore several modules may have the same named subroutine (e.g. INIT, FINI, DOWORK, ...). I do suggest the programmer prepend the module name decoration to the function name on the C++ side. The can then choose to either encapsulate the call or use #define to redirect the call. I suppose they could address the C++ standards committee to introduce the Fortran like attribute of ALIAS :~)

Jim Dempsey
0 Kudos
carlls
Beginner
701 Views
Quoting - tim18
Such as subroutine my_sub() bind(c) ? (look up use iso_c_binding)

I forgot to mention fornow I am stuck withIFort 9.1, I have IFort 11 but can't use it for this release
0 Kudos
Jugoslav_Dujic
Valued Contributor II
701 Views
Quoting - carlls

I forgot to mention fornow I am stuck withIFort 9.1, I have IFort 11 but can't use it for this release

Use ALIAS attribute. Even if you want to call the routines from other Fortran units, it will be transparent because it's in module (i.e. has an explicit interface).

[cpp]subroutine my_sub()
!DEC$ATTRIBUTES DECORATE, ALIAS: "my_sub":: my_sub[/cpp]


0 Kudos
IanH
Honored Contributor II
701 Views

Aproblem with this is the module may name the subroutine in a common manner and therefore several modules may have the same named subroutine (e.g. INIT, FINI, DOWORK, ...). I do suggest the programmer prepend the module name decoration to the function name on the C++ side. The can then choose to either encapsulate the call or use #define to redirect the call. I suppose they could address the C++ standards committee to introduce the Fortran like attribute of ALIAS :~)

Jim Dempsey

Change the binding label of the module procedure on the Fortran side to be unique.

MODULE xxx
...
CONTAINS
SUBROUTINE INIT, BIND(C, NAME='Really_Unique_Name_To_Specifically_Call_This_Proc_From_C')
...

(Requires C-interoperability, so not applicable for IVF 9.x)
0 Kudos
jimdempseyatthecove
Honored Contributor III
701 Views
Quoting - IanH

Change the binding label of the module procedure on the Fortran side to be unique.

MODULE xxx
...
CONTAINS
SUBROUTINE INIT, BIND(C, NAME='Really_Unique_Name_To_Specifically_Call_This_Proc_From_C')
...

(Requires C-interoperability, so not applicable for IVF 9.x)

Thanks, that is much better than pre-pending the "MOD_FOO_mp_" to the C++ side as this may be subject to change whereas the BIND(C,NAME="...") will not. Assuming you have the newer IVF (good reason to upgrade eh).

Jim
0 Kudos
Reply