Fortran Coder
标题: 如何实现fortran调c,同时c调fortran [打印本页]
作者: 苏坡,man 时间: 2016-1-8 18:58
标题: 如何实现fortran调c,同时c调fortran
程序如下:我的qq是 869083616
我的问题是:如何将fortran 的子程序ss传递给fortran程序需要调用的c函数print_a_?
testc.c:[C] syntaxhighlighter_viewsource syntaxhighlighter_copycode
#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] syntaxhighlighter_viewsource syntaxhighlighter_copycode
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
作者: pasuka 时间: 2016-1-8 22:33
gcc系列的话,参考这个
https://gcc.gnu.org/onlinedocs/g ... tines-and-Functions
https://gcc.gnu.org/onlinedocs/g ... rking-with-Pointers
https://gcc.gnu.org/onlinedocs/g ... y-of-Fortran-with-C
ivf+VC的话,参考ivf帮助文档的混合编程章节
作者: fcode 时间: 2016-1-9 13:35
[C] syntaxhighlighter_viewsource syntaxhighlighter_copycode
#include <stdio.h>
typedef int(*pSSptr) (int *a);
void print_a_( pSSptr ss_ ,int *a ){
ss_( a ); //调用传递进来的子程序
printf( "ca=%d\n" , *a );
}
[Fortran] syntaxhighlighter_viewsource syntaxhighlighter_copycode
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
欢迎光临 Fortran Coder (http://bbs.fcode.cn/) |
Powered by Discuz! X3.2 |