Search code examples
compilationfortrangfortran

Compile Module and Main Program In the Same File Using GFortran?


I am new to fortran and I have this fortran90 program I am trying to run where the module and the main are in the same file called main.f90:

module real_precision
    implicit none

    integer, parameter :: sp = selected_real_kind(1)
    integer, parameter :: dp = selected_real_kind(15)

end module real_precision

program main_program

    use real_precision

    implicit none

    real(sp) :: a = 1.0_sp
    real(dp) :: b = 1.0_dp

    print *, a
    print *, b

end program main_program

And I compiled it once doing:

gfortran main.f90 -o main.x

Then run it:

./main.x

However I made a change to the module and saved it but compiling and running it this same way provides the same output which leads me to think that the module needs to be compiled? How do I compile both where they're in the same file? I could make the module a separate file but I'd like to know how to do it this way!


Solution

  • selected_real_kind(p) returns the kind parameter of a real with precision at least p digits (if one exists). It does not give a kind parameter for a real with exactly that precision.

    If your compiler has does not have a real with precision less than q then selected _real_kind(q) and selected_real_kind(q-1) will not return different kind parameters.