- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Link Copied
6 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - jimdempseyatthecove
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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