Import frontend module procedure from fortran into C

How do I import fortran interface

into C using extern

?

Suppose I have the following fortran module:

!swapper module
module swapper
   interface swap
      module procedure swap_r, swap_i, swap_c
   end interface
contains
    !subroutine implementations here:
    !swap_r takes two double precision as argument
    !swap_i takes two integers as argument
    !swap_c takes two characters as argument
end module swapper

      

Then I can just do in C, the following?

extern "C" void __swapper_MOD_swap(double*, double*)
extern "C" void __swapper_MOD_swap(int*, int*)
extern "C" void __swapper_MOD_swap(char*, char*)

      

Alternatively, if I promise to swap

only call with double precision numbers, can I do this exclusively?

extern "C" void __swapper_MOD_swap(double*, double*)

      

0


source to share


1 answer


It looks like you are actually using C ++. But first, let's answer in C or C ++ style:

No you can't do

extern "C" void __swapper_MOD_swap(double*, double*)

      

you cannot do this for three different types of arguments, you cannot even do this for one type of arguments.

In fact, there shouldn't be any __swapper_MOD_swap

in the library.

What Fortran does is that it maintains an interface (which is just a description of how to call something) to three special subroutines swap_r, swap_i, swap_c

and allows you to call it by name swap

.

But there is no real subroutine in the module swap

.
Fortran will simply allow you to name these three specifications under a different name, that's all.



It is not possible to call these functions from C as generic. C does not have this kind of generics! (It has some functionality that works with any type using void*

).


In C ++, you can create generics specialized for different types, and you can call Fortran procedures as generic, but you have to tell that to C ++ yourself! You can create a template and manually customize that template to call the appropriate functions extern "C"

.

For an example see my title https://github.com/LadaF/PoisFFT/blob/master/src/poisfft.h where I create a C ++ class that is linked to extern "C" struct

and some extern "C"

functionality that is however implemented in Fortran ...


Finally, DO NOT use symbols __swapper_MOD_

. Create Fortran bindings with bind(C,name="some_name")

and call some_name

through extern "C"

, because different Fortran compilers use different name translation schemes.

+1


source







All Articles