Fortran Coder

标题: 求助:关于彭国伦fortran95上用fortran调用c例题 [打印本页]

作者: 风和    时间: 2021-10-17 11:29
标题: 求助:关于彭国伦fortran95上用fortran调用c例题
正在学习fortran与c语言混编,完全根据教材上的案例上的代码进行编辑,为什么老报错?使用的是ivf2010编辑,恳请各位大佬给个能调用c的方法。
[Fortran] 纯文本查看 复制代码
module cprog
  interface
    subroutine SUBA(a)
    !DEC$ ATTRIBUTES C, ALIAS:'_sub1' :: SUBA
        integer :: a
        !DEC$ ATTRIBUTES REFERENCE :: a
        end subroutine
    subroutine SUBB(a)
    !DEC$ ATTRIBUTES C, ALIAS:'_sub2' :: SUBB
        integer :: a
        !DEC$ ATTRIBUTES VALUE :: a
        end subroutine
  end interface
end module cprog

program main
  use cprog
  implicit none
  integer(8)  :: a=10

  call SUBA(a)
  call SUBB(a)

  stop
end program

[C++] 纯文本查看 复制代码


#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

void sub1(int *num)
{
        printf("%d\n",*num);
}

void sub2(int num)
{
        printf("%d\n",num);
}

#ifdef __cplusplus
}
#endif



EX1202FORTRAN代码.txt (456 Bytes, 下载次数: 0) EX1202C代码 .txt (199 Bytes, 下载次数: 0)






作者: fcode    时间: 2021-10-17 18:27
D:\OurDoc\Desktop\sdw>cl -c a.c -nologo
a.c

D:\OurDoc\Desktop\sdw>ifort f.f90 a.obj -nologo

D:\OurDoc\Desktop\sdw>f
10
10

D:\OurDoc\Desktop\sdw>


[Fortran] 纯文本查看 复制代码
module cprog
  interface
    subroutine SUBA(a) Bind(C,Name="sub1")
        integer(8) :: a
        end subroutine
    subroutine SUBB(a) Bind(C,Name="sub2")
        integer(8) , value:: a
        end subroutine
  end interface
end module cprog

program main
  use cprog
  implicit none
  integer(8)  :: a=10

  call SUBA(a)
  call SUBB(a)

  stop
end program

作者: 风和    时间: 2021-10-17 21:48
本帖最后由 风和 于 2021-10-17 21:53 编辑

然而,仍然会报上述错误,有没有可能是,我编译的lib是32位的,还是我的设置有问题,已经卡好多天了
作者: fcode    时间: 2021-10-17 22:44
我亲自测了是OK的,你遇到问题的话,应该是一些地方没有设置正确,或者操作上有问题。

论坛里对于操作问题不是很容易沟通,你可以加我们的QQ群:2338021,找我(臭石头雪球)
作者: li913    时间: 2021-10-20 12:12
https://www.bilibili.com/video/BV1XD4y1S7jz?p=6




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