好的,谢谢啦 |
[Fortran] 纯文本查看 复制代码 Program void use ISO_C_BINDING implicit none interface subroutine print_a(s,a) bind(C,name='print_a_') import type(C_FUNPTR) , value :: s type(C_PTR) , value :: a end subroutine print_a subroutine ss(a) Bind( c , Name = "ss_" ) implicit none integer a end subroutine ss end interface integer , target :: a = 256 call print_a( c_funloc(ss) , c_loc(a) ) print * , a end program void !************************************ ! 需要传递的子程序如下: !************************************ subroutine ss(a) Bind( c , Name = "ss_" ) implicit none integer a a = a + 1 print *,'a=',a end subroutine ss c语言代码(请把 C 语言代码的扩展名改为 .c 而不是 .cpp) [C] 纯文本查看 复制代码 #include <stdio.h> extern void ss_(int a); //我自己写的传递进来的ss子程序 void print_a_(void* t, int a){ // int b=(int)t; ss_(a); //调用传递进来的子程序 // printf("ct_=%p\n",t); printf("ca=%d\n", a); } |
不需要额外设置,在这个例子里。你需要把 C 语言编译出来的 obj 放入工程,才能链接。 并且这个例子是 fortran (program main)调用 c 语言(print_a),然后c语言又调用fortran( subroutine ss) |
zhuhuanlai 发表于 2017-1-3 12:45 可能版本问题吧 我用的VS2010 fortran2011 一运行出现了好多错误呀 |
楼上的例子非常的古老,建议用新的语法 ISO_C_Binding 实现混编。 本论坛有很多例子: http://bbs.fcode.cn/forum.php?mo ... ypeid&typeid=16 |
捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )
GMT+8, 2024-5-5 09:58