您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > C++ Fortran混合编程
C/C++/Fortran混合编程浅谈(一)直接链接方式现今流行很多编程语言,在编译型语言中,C/C++/Fortran语言应用非常广泛,C以其效率高效底层操作为著称,C++以其很好的面向对象类框架泛型编程为特点,Fortran则以现世存有大量的计算程序而占有重要的位置,在编程中,集合他们三者的长处是个很好的做法。混合编程有很多方法,以下介绍一下基本方法。对于各个编译器,如果编译中间的二进制文件.o或.obj的结构相同,则可以直接链接混合编程。遵循约定:C/C++默认传值,Fortran传址。一、相同编译器家族以gcc家族为例,类似的还有IntelCCompiler和IntelFortranCompiler等。1、C和Fortran(1)C调用Fortranmain.c#includestdio.hvoidsub_fortran_(int*,float*,double*);doublefunction_fortran_(double*);intmain(){intnum_int;floatnum_float;doublenum_double;doublenum;num_int=3;num_float=5.0;sub_fortran_(&num_int,&num_float,&num_double);num=function_fortran_(&num_double);printf(num_int=%d\nnum_float=%f\nnum_double=%f\nnum=%f,num_int,num_float,num_double,num);return0;}sub.f90subroutineSub_Fortran(NumInt,NumFloat,NumDouble)implicitnoneinteger::NumIntreal::NumFloatreal(8)::NumDoubleNumDouble=NumFloat**NumIntendsubroutinereal(8)functionFunction_Fortran(NumDouble)implicitnonereal(8)::NumDoubleFunction_Fortran=sqrt(NumDouble)endfunctionsub.f90(F2003方式)subroutineSub_Fortran(NumInt,NumFloat,NumDouble)useISO_C_BINDINGimplicitnoneinteger(c_int)::NumIntreal(c_float)::NumFloatreal(c_double)::NumDoubleNumDouble=NumFloat**NumIntendsubroutinereal(c_double)functionFunction_Fortran(NumDouble)useISO_C_BINDINGimplicitnonereal(c_double)::NumDoubleFunction_Fortran=sqrt(NumDouble)endfunction链接方法gcc–omain.o–cmain.cgfortran–osub.o–csub.f90gcc–omain.exemain.osub.o或者直接gcc–omain.exemain.csub.f90输出(2)Fortran调用Cmain.f90programmainimplicitnoneinterfacesubroutinesub_c(n1,n2,n3)integer::n1real::n2real(8)::n3endsubroutinereal(8)functionfunc_c(n3)real(8)::n3endfunctionendinterfaceinteger::n1real::n2real(8)::n3,n4n1=3n2=5.0callsub_c(n1,n2,n3)n4=func_c(n3)write(*,*)n1=,n1write(*,*)n2=,n2write(*,*)n3=,n3write(*,*)n4=,n4endprogrammain.f90(F2003方式)programmainuseISO_C_BINDINGimplicitnoneinterfacesubroutinesub_c(n1,n2,n3)useISO_C_BINDINGinteger(c_int)::n1real(c_float)::n2real(c_double)::n3endsubroutinereal(c_double)functionfunc_c(n3)useISO_C_BINDINGreal(c_double)::n3endfunctionendinterfaceinteger(c_int)::n1real(c_float)::n2real(c_double)::n3,n4n1=3n2=5.0callsub_c(n1,n2,n3)n4=func_c(n3)write(*,*)n1=,n1write(*,*)n2=,n2write(*,*)n3=,n3write(*,*)n4=,n4endprogramsub.c#includemath.hvoidsub_c_(int*,float*,double*);doublefunc_c_(double*);voidsub_c_(int*n1,float*n2,double*n3){*n3=pow(*n2,*n1);}doublefunc_c_(double*n3){doublen4;n4=sqrt(*n3);returnn4;}链接方式gcc–osub.osub.cgfortran–omain.omain.f90gfortran–omain.exemain.osub.o或是直接gfortran–omain.exemain.f90sub.c输出visualC++与FORTRAN混合编程(2010-04-2214:15:25)转载标签:visualcfortran混合编程分类:科研原文:本文示例说明用C++编写的主程序调用采用FORTRAN语言编写的子程序的方法。方法一:直接在MicrosoftDeveloperStudio创建包含c语言和FORTRAN语言的工程。创建win32console工程,分别添加cmain.cpp和forsubs.for两个文件,编译链接。#includestdio.hexternC{int_stdcallFACT(intn);}externC{void_stdcallPYTHAGORAS(floata,floatb,float*c);}main(){floatc;printf(Factorialof7is:%d\n,FACT(7));PYTHAGORAS(30,40,&c);printf(Hypotenuseifsides30,40is:%f\n,c);}CFileFORSUBS.FORCINTEGER*4FUNCTIONFact(n)INTEGER*4n[VALUE]INTEGER*4i,amtamt=1DOi=1,namt=amt*iENDDOFact=amtENDSUBROUTINEPythagoras(a,b,c)REAL*4a[VALUE]REAL*4b[VALUE]REAL*4c[REFERENCE]c=SQRT(a*a+b*b)write(*,*)你成功了!END方法二:把FORTRAN子程序做成动态链接库供VC主程序显式调用。(1)创建FORTRANDLL工程,生成forsubs.dll文件供调用。!forsubs.f90!!FUNCTIONS/SUBROUTINESexportedfromFORSUBS.dll:!FORSUBS-subroutine!INTEGER*4FUNCTIONFact(n)!DEC$ATTRIBUTESDLLEXPORT::FactINTEGER*4n[VALUE]INTEGER*4i,amtamt=1DOi=1,namt=amt*iENDDOFact=amtwrite(*,*)Mixedcallssucceed!ENDSUBROUTINEPythagoras(a,b,c)!DEC$ATTRIBUTESDLLEXPORT::PythagorasREAL*4a[VALUE]REAL*4b[VALUE]REAL*4c[REFERENCE]c=SQRT(a*a+b*b)END(2)创建win32consoleapplication,调用forsubs.dll。//C++显式调用FORTRAN动态链接库#includestdio.h#includeiostream.h#includewindows.hmain(){//声明调用约定typedefint(_stdcall*FACT)(intn);typedefvoid(_stdcall*PYTHAGORAS)(floata,floatb,float*c);//加载动态库文件HINSTANCEhLibrary=LoadLibrary(forsubs.dll);if(hLibrary==NULL){coutcan'tfindthedllfileendl;return-1;}//获得Fortran导出函数FACT的地址FACTfact=(FACT)GetProcAddress(hLibrary,FACT);if(fact==NULL){coutcan'tfindthefunctionfile.endl;return-2;}//获得Fortran导出函数PYTHAGORAS的地址PYTHAGORASpythagoras=(PYTHAGORAS)GetProcAddress(hLibrary,PYTHAGORAS);if(pythagoras==NULL){coutcan'tfindthefunctionfile.endl;return-2;}floatc;printf(Factorialof7is:%d\n,fact(7));pythagoras(30,40,&c);printf(Hypotenuseifsides30,40is:%f\n,c);FreeLibrary(hLibrary);//卸载动态库文件return0;}方法三:把FORTRAN子程序做成库文件供VC主程序隐式调用。(1)创建FORTRANDLL工程,生成forsubs.dll和forsubs.lib文件供调用。!forsubs.f90!!FUNCTIONS/SUBROUTINESexportedfromFORSUBS.lib:!FORSUBS-subroutine!INTEGER*4FUNCTIONFact(n)!ms$if.not.defined(LINKDIRECT)!ms$attributesdllexport::Fact!ms$endifINTEGER*4n[VALUE]INTEGER*4i,amtamt=1DOi=1,namt=amt*iENDDOFact=amtwrite(*,*)Mixedcallssucceed!ENDSUBROUTINEPythagoras(a,b,c)!ms$if.not.defined(LINKDIRECT)!ms$attributesdllexport::Pythagoras!ms$endifREAL*4a[VALUE]REAL*4b[VALUE]REAL*4c[REFERENCE]c=SQRT(a*a+b*b)END(2)创建win32consoleapplication,添加forsubs.lib到工程,动态调用forsubs.dll。#includestdio.hexternC{int_stdcallFACT(intn);}externC{void_stdc
本文标题:C++ Fortran混合编程
链接地址:https://www.777doc.com/doc-4194926 .html