怎样用c调用fortran的动态链接库

如题所述

C语言和Fortran混合编程借助于Fortran生成的DLL进行(采用C默认的传址方式进行函数参数传递)方法和实例:
1.Fortran
生成DLL
新建Fortran
DLL程序test1.f
添加如下代码:
! test1.f90

! FUNCTIONS/SUBROUTINES
exported from test1.dll:
! test1 -
subroutine
!示例没有返回值的子例程
subroutine
test1(a,b)
! Expose subroutine test1 to
users of this DLL

!DEC$ ATTRIBUTES
C,DLLEXPORT::test1
! Variables
! Body of
test1
integer a,b
integer sum
sum=a+b
return
end subroutine
test1
!示例有返回值的整数四则运算
!两数相加
function
add(a,b)
implicit none
!DEC$ ATTRIBUTES
C,DLLEXPORT::add
integer
a,b,add
add=a+b
return
end
!两数相减
function
abstract(a,b)
implicit none
!DEC$ ATTRIBUTES
C,DLLEXPORT::abstract
integer
a,b,abstract
abstract=a-b
return
end
!两数相乘
function
multiply(a,b)
implicit none
!DEC$ ATTRIBUTES
C,DLLEXPORT::multiply
integer
a,b,multiply
multiply=a*b
return
end
!两数相除
(需要添加考虑被除数是否为0以及能否整除的判断)
function
divided(a,b)
implicit none
!DEC$ ATTRIBUTES
C,DLLEXPORT::divided
integer
a,b,divided
divided=a/b
return
end
编译后生成test1.dll,test1.obj等文件。其中这两个文件是我们在VC中调用所需要的。
温馨提示:答案为网友推荐,仅供参考
相似回答