CFortran混合编程

上传人:新** 文档编号:431956753 上传时间:2023-06-26 格式:DOC 页数:31 大小:335.50KB
返回 下载 相关 举报
CFortran混合编程_第1页
第1页 / 共31页
CFortran混合编程_第2页
第2页 / 共31页
CFortran混合编程_第3页
第3页 / 共31页
CFortran混合编程_第4页
第4页 / 共31页
CFortran混合编程_第5页
第5页 / 共31页
点击查看更多>>
资源描述

《CFortran混合编程》由会员分享,可在线阅读,更多相关《CFortran混合编程(31页珍藏版)》请在金锄头文库上搜索。

1、C/C+/Fortran混合编程浅谈(一)直接链接方式现今流行很多编程语言,在编译型语言中,C/C+/Fortran语言应用非常广泛,C以其效率高效底层操作为著称,C+以其很好的面向对象类框架泛型编程为特点,Fortra n则以现世存有大量的计算程序而占有重要的位置,在编程中,集合他们三者的长处是个很好的做法。混合 编程有很多方法,以下介绍一下基本方法。对于各个编译器,如果编译中间的二进制文件.o或.obj的结构相同,则可以直接链接混合编程。遵循约定:C/C+默认传值,Fortra n 传址。、相同编译器家族等。以 gcc 家族为例,类似的还有Intel C Compiler 和 In tel

2、 Fortran Compiler 1、C 和 Fortran(1) C 调用 Fortran main .c#i ncludevoid sub_fortran_(int *, float *, double *);double function_fortran_(double *);int main()int num_int;float num_float;double nu m_double;double num;num_in t=3;nu m_float=5.0;sub_fortra n_(&numnt,&num _float,&num _double);num=fun ctio n_f

3、ortra n_(&num _double);printf( numnt=%dnnum_float=%fnnum_double =%fnnum=%f,numnt,num _float, num _double,num);return 0;sub.f90subrouti neSub_Fortra n(N uml nt,NumFloat,NumDouble)implicit nonein teger : Numlntreal : NumFloatreal(8) : NumDoubleNumDouble=NumFloat*NumI ntend subrout inereal(8) fun ctio

4、nFun ctio n_ Fortra n(N umDouble)implicit nonereal(8) : NumDoubleFunction_Fortran=sqrt (NumDouble)end fun cti onsub.f90(F2003 方式)subrouti neSub_Fortra n(N uml nt,NumFloat,NumDouble)use ISO_C_BINDINGimplicit noneinteger (c_int) : Numlntreal (c_float) : NumFloatreal (c_double) : NumDoubleNumDouble=Num

5、Float*NumI nt end subrout inereal (c_double) functionFun ctio n_ Fortra n(N umDouble)use ISO_C_BINDINGimplicit nonereal (c_double) : NumDoubleFunction_Fortran=sqrt (NumDouble)end fun cti on链接方法gcc mai n.o-c mai n.cgfortra no sub.o c sub.f90 gcc mai n.exe mai n.o sub.o或者直接gcc -o main.exe main.c sub.f

6、90输岀E=teinnum_int =3 num_float =5 000000 num_double =12S-003000 num=11_180340Fortran调用Cmai n.f90program mainimplicit nonein terfacesubrout inesub_c (n1,n2,n3)in teger : n1 real : n2 real(8) : n3end subrout inereal(8) fun cti onfun c_c( n3)real(8) : n3end functionend in terfacein teger : n1real : n2r

7、eal(8) : n3, n4n1=3n2=5.0call sub_c(n1,n2,n3)n4=func_c(n3)write (*,*)n 1=,n1write (*,*)n 2=,n2write (*,*)n 3=,n3write (*,*)n 4=,n4end programmai n. f90(F2003方式)program mai nuse ISO_C_BINDING implicit none in terfacesubrout inesub_c (n1,n2,n3)use ISO_C_BINDINGinteger (c_int) : n1real (c_float) : n2re

8、al (c_double) : n3end subrout inereal (c_double)function func_c(n3)use ISO_C_BINDINGreal (c_double) : n3end functionend in terfaceinteger (c_int) : n1real (c_float) : n2real (c_double) : n3,n4n1=3n2=5.0call sub_c(n1,n2,n3)n4=func_c(n3)write (*,*) n 1= ,n1write (*,*) n2= ,n2write (*,*) n3= ,n3write (

9、*,*) n4= ,n4end programsub.c#in clude void sub_c_( int *, float *, double *);double func_c_( double *);void sub_c_( int *n1, float *n2, double *n3) *n 3=pow(* n2,* n1);double func_c_( double *n3)double n4;n 4=sqrt(* n3);return n4;gfortra n链接方式gcc sub.o sub.cgfortra n-o mai n.o main .f90gfortra no ma

10、i n.exe mai n.o sub.o或是直接o mai n. exe mai n.f90 sub.c输岀|E*XtXtempiTiain1nl =3n2 -n3 =125 JRI00000000I00060n4-丄1.180339687498949visual C+ 与 FORTRAN!合编程(2010-04-22 14:15:25)转载标签:分类:科研visualcfortran混合编程原文:http:/ Microsoft Developer Studio创建包含c语言和 FORTRA语言的工程。创建wi n32 con sole工程,分别添加cma in .cpp和forsubs.

11、for 两个文件,编译 链接。#i nclude extern C int _stdcall FACT (int n);extern C void _stdcall PYTHAGORAS (float a, float b, float *c); mai n()float c;printf(Factorial of 7 is: %dn, FACT(7);PYTHAGORAS (30, 40, & c);printf(Hypotenuse if sides 30, 40 is: %fn, c);C File FORSUBS.FORCINTEGER*4 FUNCTION Fact (n)INTEG

12、ER*4 n VALUEINTEGER*4 i, amtamt = 1DO i = 1, namt = amt * iEND DOFact = amtENDSUBROUTINE Pythagoras (a, b, c)REAL*4 a VALUEREAL*4 b VALUEREAL*4 c REFERENCEc = SQRT (a * a + b * b) write(*,*) 你成功了! END方法二:把FORTRA子程序做成动态链接库供VC主程序显式调用 创建FORTRAN DL工程,生成forsubs.dll文件供调用。! forsubs.f90! FUNCTIONS/SUBROUTIN

13、ES exported from FORSUBS.dll:! FORSUBS - subroutine!INTEGER*4 FUNCTION Fact (n)!DEC$ ATTRIBUTES DLLEXPORT:FactINTEGER*4 n VALUEINTEGER*4 i, amtamt = 1 DO i = 1, namt = amt * iEND DOFact = amt write(*,*)Mixed calls succeed!ENDSUBROUTINE Pythagoras (a, b, c)!DEC$ ATTRIBUTES DLLEXPORT:PythagorasREAL*4

14、a VALUEREAL*4 b VALUEREAL*4 c REFERENCEc = SQRT (a * a + b * b)END(2) 创建 win32 console application ,调用 forsubs.dll 。/C+显式调用FORTRA动态链接库#include #include #include main()/ 声明调用约定typedef int (_stdcall * FACT)(int n);typedef void (_stdcall * PYTHAGORAS)(float a, float b, float *c);/ 加载动态库文件HINSTANCE hLibrary=LoadLibrary(forsubs.dl

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号