linking multiple files with fortran
Asked Answered
C

1

5

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
Cassimere answered 10/6, 2014 at 19:48 Comment(2)
You are passing radius as a subroutine argument, which is fine. You should include the code for one of the subroutines such as programA and what the output of your program is. Have you used subroutines before? I suggest you find an example of how a subroutine is used in an online tutorial.Nasser
my subroutines are working fine---I can get the code i'm working on to give me everything I want when I do it with subroutines. I'm just worried that it could become hard to navigate when I have a large amoutn of them and I'll undergo issues. Though if using subroutines is the best way to do this kind of thing I will keep doing it then. I'll include one of my subroutines to make things more clear.Cassimere
U
10

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.

Unit answered 10/6, 2014 at 20:27 Comment(1)
thanks---I really needed a direction to try to look into. I'll research into makefiles, they sound like what I need.Cassimere

© 2022 - 2024 — McMap. All rights reserved.