Fortran Coder

标题: C调用Fortran dll [打印本页]

作者: dongtian99    时间: 2017-1-3 11:48
标题: C调用Fortran dll
谁有C调用Fortran dll的例题呀,最好可以运行,学习一下。自己尝试做了一个例题,出错了:  错误        1        error LNK2019: 无法解析的外部符号 "void __stdcall OUTPUT(int *,int *,int *)" (?OUTPUT@@YGXPAH00@Z),该符号在函数 _main 中被引用        D:\Personal\Desktop\for4c\main\main.obj        main



作者: zhuhuanlai    时间: 2017-1-3 12:45
http://www.3lian.com/edu/2012/04-14/25509.html
有比较详细的例子
作者: fcode    时间: 2017-1-3 13:06
楼上的例子非常的古老,建议用新的语法 ISO_C_Binding 实现混编。
本论坛有很多例子:
http://bbs.fcode.cn/forum.php?mo ... ypeid&typeid=16
作者: dongtian99    时间: 2017-1-3 19:29
本帖最后由 dongtian99 于 2017-1-3 19:31 编辑
fcode 发表于 2017-1-3 13:06
楼上的例子非常的古老,建议用新的语法 ISO_C_Binding 实现混编。
本论坛有很多例子:
http://bbs.fcode.cn ...

您好,使用ISO_C_Binding,是不是还得设置一下呀?我用的VS2010 Fortran2011,试了一下论坛中的小程序,结果出错
Program voi0d
  use ISO_C_BINDING
  implicit none
  external :: ss
  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
  end interface
  integer , target :: a = 256
  call print_a( c_funloc(ss) , c_loc(a) )
  print * , a
End Program voi0d
!************************************
    !  需要传递的子程序如下:
!************************************
   subroutine ss(a) Bind( c )
    implicit none
    integer a
    a = a + 1
    print *,'a=',a
   end subroutine ss
D:\Personal\Desktop\tupian1
D:\Personal\Desktop\tupian2


tupian1.jpg (16.94 KB, 下载次数: 286)

tupian1.jpg

tupian2.png (20.1 KB, 下载次数: 276)

tupian2.png

作者: dongtian99    时间: 2017-1-3 19:52
zhuhuanlai 发表于 2017-1-3 12:45
http://www.3lian.com/edu/2012/04-14/25509.html
有比较详细的例子

可能版本问题吧 我用的VS2010 fortran2011  一运行出现了好多错误呀
作者: vvt    时间: 2017-1-3 20:09
不需要额外设置,在这个例子里。你需要把 C 语言编译出来的 obj 放入工程,才能链接。

并且这个例子是 fortran (program main)调用 c 语言(print_a),然后c语言又调用fortran( subroutine ss)
作者: dongtian99    时间: 2017-1-3 20:34
vvt 发表于 2017-1-3 20:09
不需要额外设置,在这个例子里。你需要把 C 语言编译出来的 obj 放入工程,才能链接。

并且这个例子是 for ...

还是有问题呀,刚接触这个,您能帮我看一下吗


图片1.png (14.42 KB, 下载次数: 276)

图片1.png

图片2.png (31.82 KB, 下载次数: 273)

图片2.png

图片3.png (11.54 KB, 下载次数: 284)

图片3.png

图片4.png (30.58 KB, 下载次数: 303)

图片4.png

作者: vvt    时间: 2017-1-3 21:06
[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);
}

作者: dongtian99    时间: 2017-1-4 09:23
好的,谢谢啦




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2