Calling C++ Class methods from Fortran

Calling C++ Class methods from Fortran

I'm just trying to clarify whether or not it is possible to call a member function of a C++ Object from Fortran.

I'm assuming it's not possible but the way to get around it, would be to call a regular static C function which would then be used as a link between the desired Object's method and Fortran.

Thanking whoever can clarify this in advance,

2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

As you probably know, C++ member functions have this as the first hidden parameter; thus, in theory, you could call them from Fortran provided you get a C pointer to the class object somehow, write a proper INTERFACE block and call the method, passing the pointer as the first argument.

Another issue could be calling conventions; AFAIK MSVC++ uses __fastcall as the default, where two arguments are passed through registers and the rest using stack. I don't know the details (and I'm not even sure about the above statement), but that cannot be emulated in VF. Thus, I think you'd be able to do it only if you declare the member functions __stdcall or __cdecl in VC++. (If it's another compiler, check the documentation about default calling convention). Note also that MSVC mangles names in a naughty way by default (e.g. ?_Foo@YAXXUZ), so your INTERFACE block must take that into account too.


Leave a Comment

Please sign in to add a comment. Not a member? Join today