用Fortran链接多个文件 [英] linking multiple files with fortran

查看:388
本文介绍了用Fortran链接多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Fortran新手,但我试图找到一种方法,可以从我编写的程序中检索信息,而不必将它们作为子程序包含在新文件中。截至目前,我在我的新文件中有4个子程序,我希望能够在所有4中输入半径并接收它们各自的输出。



this是我的代码的基本格式---基本上我想表明我需要4个独立的程序来获取当前程序表达式所需的所有变量。
到目前为止,我试图使用包含和调用表达式,但是他们无法检索到我需要的信息以将它们带回到我的文件中,并且他们想出了不适用的答案。 p>

 程序practicedynamo 

隐含无

真:: A,B,C ,半径

real :: Bsquared,Vsquared

读*,半径

调用程序A(半径,A)

调用程序B(半径,B)

调用程序C(半径,C)


Vsquared =(1.0 / 3.0)* B

Bsquared = 4 * pi * density * Vsquared

gradient = radius * C

Rvector = Bsquared * Vsquared * gradient

ThetaVector = Rvector *罪(A)

结束程序practicedynamo

!然后我的四个子程序将被放在
!以下是我的一个子程序的例子实际的代码(上面的版本已被简化,我已经更改了变量)

子例程tes tdensity(半径,密度)

隐含无

实数::半径,x,sunradius,密度

if(半径> 0.和。半径<= 695500000),那么

sunradius = 695500000

x = radius / sunradius

密度=((519 * x ** 4.0) - ( 1630 * x ** 3.0)+(1844 * x * x) - (889 * x)+155)

print *,

密度=密度* 1000

print *,密度是,密度,kg每米立方

else

print *,这个半径不是太阳选项

结束如果

结束子程序testdensity


<您尚未提及如何编译代码,但以下是将多个源文件包含在单个可执行文件中的一些常规方法。您不需要包含这些文件,您可以单独编译它们并将它们链接在一起。编写一个Makefile来做到这一点是值得推荐的,你可以在其他地方找到大量的例子。

为了将多个文件编译成一个可执行文件,编译时只需要列出所有文件

  gfortran -o输出程式A.f90程式B.f90程式C.90 mainprogram.f90 

如果您不想一起编译它们或在编译时必须重新编译,您可以编译单个对象,例如

  gfortran -c -o programA.o programA.f90 
gfortran -c -o programB.o programB.f90
gfortran - c -o programC.o programC.f90

然后链接为

  gfortran -o输出mainprogram.f90 programA.o programB.o programC.o 

如果您想要使用库并希望程序AC位于独立库中,则可以先编译上述对象,然后

  ar rcs libABC.a programA.o programB.o programC.o 

然后将主程序编译为

  gfortran -o输出mainprogram.f90 libABC.a 






如果您不使用模块,您将负责确保您对外部子例程的调用与外部文件中声明的接口相匹配。为了安全并让编译器捕捉到不匹配参数的问题,您可以在程序中声明显式接口,或者将外部代码放入模块中,在主程序中使用这些模块。 / p>

I'm new to Fortran but I'm trying to find a way that I can retrieve information from programs I've written without including them as subprograms within my new file. As of right now I have 4 subroutines within my new file and I would like to instead just be able to input the radius into all 4 and receive their respective outputs.

this is the basic format for my code--- basically I want to show that I need 4 separate programs in order to get all the variables needed for the current programs expression. So far I've tried to use both the include and call expressions but they weren't able to retrieve the information I needed to bring back into my file and they came up with just "not applicable" answers.

program practicedynamo

    implicit none

    real:: A,B,C, Radius

    real::Bsquared,Vsquared

    read*,radius

    call programA(radius,A)

    call programB(radius,B)

    call programC(radius,C)


    Vsquared=(1.0/3.0)*B

    Bsquared= 4*pi*density*Vsquared

    gradient=radius*C

    Rvector=Bsquared*Vsquared*gradient

    ThetaVector=Rvector*sin(A)

end program practicedynamo

!and then my four subroutines would be placed afterwards
!here is an example of one of my subroutines within my actual code (the version above has been simplified and I've changed the variables)

subroutine testdensity(radius,density)

implicit none

    real::radius,x,sunradius,density

   if (radius>0.and.radius<=695500000) then

        sunradius=695500000

        x=radius/sunradius

        density=((519*x**4.0)-(1630*x**3.0)+(1844*x*x)-(889*x)+155)

        print*,"                                             "

        density=density*1000

        print*,"the density is",density, "kg per meters cubed"

    else

        print*, "this radius is not an option for the sun"

    end if

end subroutine testdensity

解决方案

You haven't mentioned how you are compiling your code, but here are some general ways to include multiple source files in a single executable. You don't need to include the files, you can just compile them separately and link them together. Writing a Makefile to do this is recommended and you can find plenty of examples on that elsewhere.

To compile multiple files into one executable, you need only list them all when compiling

gfortran -o output programA.f90 programB.f90 programC.90 mainprogram.f90

If you do not want to compile them all together or have to recompile when you build, you can compile individual objects, e.g.

gfortran -c -o programA.o programA.f90
gfortran -c -o programB.o programB.f90
gfortran -c -o programC.o programC.f90

and then link as

gfortran -o output mainprogram.f90 programA.o programB.o programC.o

If you are instead trying to use libraries and want program A-C to be in a standalone library, you can first compile the objects as above, then

ar rcs libABC.a programA.o programB.o programC.o

and then compile your main program as

gfortran -o output mainprogram.f90 libABC.a 


If you aren't using modules, you'll be responsible for making sure that your calls to external subroutines match the declared interface in the external file. To be safe and have the compiler catch problems with mismatched arguments you can declare explicit interfaces in your program or put the external code into modules and use those modules in the main program.

这篇关于用Fortran链接多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆