Fortran Coder

查看: 8588|回复: 2
打印 上一主题 下一主题

[混编] 如何实现fortran调c,同时c调fortran

[复制链接]

13

帖子

8

主题

0

精华

入门

F 币
83 元
贡献
51 点
跳转到指定楼层
楼主
发表于 2016-1-8 18:58:44 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
程序如下:我的qq是 869083616
我的问题是:如何将fortran 的子程序ss传递给fortran程序需要调用的c函数print_a_?
testc.c:
[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);
   }

testf.f:
[Fortran] 纯文本查看 复制代码
           program voi0d
            !use ISO_C_BINDING
           implicit none
           external :: ss
          interface
            subroutine print_a(s,a) bind(C,name='print_a_')   !c程序接口
             external :: s
           integer a 
          end subroutine print_a
         end interface
            integer  a
            integer*4 pt,loc
            !pt = loc(ss)
            !s=4
            a = 256
            call print_a(ss,%VAL(a))  !想将子程序ss传递给c的函数print_a_,但是好像没有传入
            !call ss(a) 
            print *,pt, a
          end program
          !************************************
             !  需要传递的子程序如下:
          !************************************
            subroutine ss(a)   !需要
             !use ISO_C_BINDING
             implicit none
             integer  a
             a = a + 1
             print *,'a=',a
             return 
             !print *,'a= ',a
            end subroutine ss
分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

沙发
发表于 2016-1-8 22:33:08 | 只看该作者

1963

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1357 元
贡献
574 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

板凳
发表于 2016-1-9 13:35:38 | 只看该作者
[C] 纯文本查看 复制代码
#include <stdio.h>
typedef int(*pSSptr) (int *a);
void print_a_( pSSptr ss_ ,int *a ){
  ss_( a );  //调用传递进来的子程序
  printf( "ca=%d\n" , *a );
}


[Fortran] 纯文本查看 复制代码
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


您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

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

GMT+8, 2024-5-3 05:25

Powered by Tencent X3.4

© 2013-2024 Tencent

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