Fortran Coder

C调用Fortran dll

查看数: 9439 | 评论数: 8 | 收藏 0
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2017-1-3 11:48

正文摘要:

谁有C调用Fortran dll的例题呀,最好可以运行,学习一下。自己尝试做了一个例题,出错了:  错误        1        error LNK2019: 无法解析的外部符号 "void __st ...

回复

dongtian99 发表于 2017-1-4 09:23:56
好的,谢谢啦
vvt 发表于 2017-1-3 21:06:51
[Fortran] 纯文本查看 复制代码
01Program void
02  use ISO_C_BINDING
03  implicit none
04  interface
05    subroutine print_a(s,a) bind(C,name='print_a_')
06      import
07      type(C_FUNPTR) , value :: s
08      type(C_PTR) , value :: a
09    end subroutine print_a
10    subroutine ss(a) Bind( c , Name = "ss_" )
11    implicit none
12    integer a
13   end subroutine ss
14  end interface
15  integer , target :: a = 256
16  call print_a( c_funloc(ss) , c_loc(a) )
17  print * , a
18end program void
19!************************************
20    !  需要传递的子程序如下:
21!************************************
22   subroutine ss(a) Bind( c , Name = "ss_" )
23    implicit none
24    integer a
25    a = a + 1
26    print *,'a=',a
27   end subroutine ss


c语言代码(请把 C 语言代码的扩展名改为 .c 而不是 .cpp)
[C] 纯文本查看 复制代码
1#include <stdio.h>
2extern void ss_(int a);  //我自己写的传递进来的ss子程序
3void print_a_(void* t, int a){
4  //  int b=(int)t;
5  ss_(a);  //调用传递进来的子程序
6  //  printf("ct_=%p\n",t);
7  printf("ca=%d\n", a);
8}
vvt 发表于 2017-1-3 20:09:22
不需要额外设置,在这个例子里。你需要把 C 语言编译出来的 obj 放入工程,才能链接。

并且这个例子是 fortran (program main)调用 c 语言(print_a),然后c语言又调用fortran( subroutine ss)
dongtian99 发表于 2017-1-3 19:52:20
zhuhuanlai 发表于 2017-1-3 12:45
http://www.3lian.com/edu/2012/04-14/25509.html
有比较详细的例子

可能版本问题吧 我用的VS2010 fortran2011  一运行出现了好多错误呀
fcode 发表于 2017-1-3 13:06:14
楼上的例子非常的古老,建议用新的语法 ISO_C_Binding 实现混编。
本论坛有很多例子:
http://bbs.fcode.cn/forum.php?mo ... ypeid&typeid=16
zhuhuanlai 发表于 2017-1-3 12:45:49

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2025-5-5 17:44

Powered by Discuz! X3.4

© 2013-2025 Comsenz Inc.

快速回复 返回顶部 返回列表