将接口模块过程从fortran导入C [英] importing interface module procedure from fortran into C
问题描述
如何使用extern
将fortran interface
导入到C中?
How should I import a fortran interface
into C using extern
?
假设我有以下fortran模块:
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
那么我可以在C语言中执行以下操作吗?
Then can I 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*)
或者,如果我保证只用双精度数字调用swap
,我可以专门这样做吗?
Alternatively, if I promise to call swap
with only double precision numbers, may I exclusively do this?
extern "C" void __swapper_MOD_swap(double*, double*)
推荐答案
看起来您实际上正在使用C ++.但是首先让我们为C或C风格的C ++回答:
It looks like you are actually using C++. But first lets answer for C or C-style C++:
不,你不能做
extern "C" void __swapper_MOD_swap(double*, double*)
您无法针对三种不同类型的参数执行此操作,即使对于单一类型的参数也无法执行此操作.
you cannot do it for three different types of arguments, you cannot do it even for a single type of arguments.
首先,库中实际上应该没有任何__swapper_MOD_swap
.
There actually should not be any __swapper_MOD_swap
in the library in the first place.
Fortran所做的是,它为三个特定的子例程swap_r, swap_i, swap_c
保留了一个接口(这只是如何调用某事物的描述),并允许您使用名称swap
进行调用.
What Fortran does is that it keeps an interface (which is just a description how to call something) for the three specific subroutines swap_r, swap_i, swap_c
and lets you call it by name swap
.
但是该模块中没有实际的子例程swap
! Fortran只允许您使用不同的名称来调用这三个细节,仅此而已.
But there is NO actual subroutine swap
in the module!!! Fortran will just let you call those three specifics under a different name, that's all.
没有办法像普通方法一样从C调用这些函数. C没有这种类型的泛型!(它确实有一些功能可以使用void*
在任何类型上运行).
There is no way how to call those functions from C like a generic. C does not have this type of generics! (It does have some functions which operate on any type using void*
).
在C ++中,您实际上可以制作专门针对不同类型的泛型,并且可以将Fortran过程称为泛型,但是您必须自己告诉C ++!您可以制作一个模板,然后手动对该模板进行特殊化处理,以调用相应的extern "C"
函数.
In C++ you can actually make generics which are specialized for different types and you can call the Fortran procedures as a generic, but you have to tell that to C++ yourself! You can make a template and manually specialize this template to call the appropriate extern "C"
functions.
例如,请参阅我的标题 https://github.com/LadaF/PoisFFT/blob/master/src/poisfft.h ,在这里我创建了一个C ++类,该类链接到extern "C" struct
和一些extern "C"
函数,但是这些函数都在Fortran中实现.
For an example see my header https://github.com/LadaF/PoisFFT/blob/master/src/poisfft.h where I make a C++ class which is linked to an extern "C" struct
and some extern "C"
functions which are however all implemented in Fortran.
最后,请勿使用__swapper_MOD_
符号.使用bind(C,name="some_name")
创建一些Fortran绑定,并通过extern "C"
调用some_name
,因为不同的Fortran编译器确实使用不同的名称处理方案.
Finally, DO NOT use the __swapper_MOD_
symbols. Create some Fortran binding using bind(C,name="some_name")
and call some_name
through extern "C"
, because different Fortran compilers DO use different name mangling schemes.
这篇关于将接口模块过程从fortran导入C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!