Topic: Problem with modules

I'm new to modern Fortran, and to Simply Fortran, which I downloaded a few days ago.  Here's my problem.  I built the following module:

module months
    implicit none
    integer, parameter, dimension(12) :: days_per_month = [31,28,31,30,31,30,31,31,30,31,30,31]
    character(len = 9), parameter, dimension(12) :: month_names = [character(len=9) :: 'January','February','March','April','May',&
end module

The following program compiles and runs:

program test
use months
implicit none

write(*,*) days_per_month(1)

end program

but this program won't even compile:

program test
use months
implicit none

integer :: i

do i = 1,12
write(*,*) days_per_month(i)
end do

end program

The error message that I get is:

Generating target.exe
build\testmain.o:testmain.f90:(.rdata$.refptr.__months_MOD_days_per_month[.refptr.__months_MOD_days_per_month]+0x0): undefined reference to `__months_MOD_days_per_month'
collect2.exe: error: ld returned 1 exit status
Error: Last command making (target.exe) returned a bad status
Error: Make execution terminated

* Failed *

The file is saved as test.f90, and I did not change any compiler defaults.  What am I doing wrong??

Re: Problem with modules

It sounds like you're not including both Fortran source files in your project in the second case.  The compiler (the linker, to be technically correct), cannot find the definition of the procedure.  I'm guessing it is because you are compiling the new Fortran file in the same directory as the original (such that it sees the module's compiler definition output).  However, the linker has no reason to go looking for any particular object file unless the original source file is included in a project.

Try adding the module's source file to your project with testmain.f90 and everything should work as expected.

Just a note: *.mod files produced by the Fortran compiler do not contain any compiled code.  Rather they contain some definitions that the compiler might need when dealing with modules.  Object files contain the actual compiled code.

Jeff Armstrong
Approximatrix, LLC