title: Compiling fortran90 modules: .mod vs .o
body: <p>I have a main program whose name is main_KMC.f90 and a directory with name modules. In the directory I have the modules that my main program needs. 
  Here you have the main_KMC.f90.</p>
  
  <pre><code>program main_KMC
  use geometry
  use search_arguments
  use mtfort90
  implicit none
  
  integer(8):: d,L,Nitt,warmSteps,measureSteps
  integer(8) :: NS,i,nn,itt
  integer(8),dimension(:,:),allocatable :: geom
  integer(8),dimension(:),allocatable :: S!Vector de spins
  
  T = get_db_arg(1,2.0d0)
  d = get_int_arg(2,int(2,8))
  L = get_int_arg(3,int(20,8))
  Nitt = get_int_arg(4,int(1d6,8))
  warmSteps = get_int_arg(5,int(1d3,8))
  measureSteps = get_int_arg(6,int(1,8))
  ....
  
  
  contains
  
  end program
  </code></pre>
  
  <p>I tried to compile it with a make file:</p>
  
  <pre><code>MAIN = main
  #MODULES = geometry mtfort search_arguments
  MODULES = $(wildcard ./modules/*.f90)
  MODULES_OBJ = $(patsubst ./modules/%.f90,%.o,$(MODULES))
  MODULES_MOD = $(patsubst ./modules/%.f90,%.mod,$(MODULES))
  F90 = gfortran
  
  
  main.out : $(MODULE_OBJ) $(MAIN).o
      $(F90) -o main.out $(MAIN).o $(MODULE_OBJ)
  
  
  $(MODULES_OBJ) : $(MODULES)
      $(F90) -c $^
  
  $(MAIN).o: $(MODULE_OBJ) $(MAIN)_KMC.f90
      $(F90) -c $(MAIN)_KMC.f90 -o $(MAIN).o
  
  .PHONY: all
  all:
      @echo $(MODULES)
      @echo $(MODULES_OBJ)
  
  .PHONY: clean
  clean:
      @rm *.o
      @rm *.mod
  
  .PHONY: modules
  modules:$(MODULES_OBJ)
  </code></pre>
  
  <p>My questions are:
  1) Which are the differences between .mod and .out. Because I tried before the make commands</p>
  
  <pre><code>$(MODULES_OBJ) : $(MODULES)
      $(F90) -c $^ -o $(MODULES_OBJ)
  </code></pre>
  
  <p>And when I execut the Makefile, appear the following error:</p>
  
  <pre><code>use geometry 
       1 
   Fatal Error: Can't open module file 'geometry.mod' for reading at (1): No such file or directory
   make: *** [main.o] Error 1
  </code></pre>
  
  <p>I thought that with the object files, you had enought to compile the program.</p>
  
  <p>The second problem that I have is with the first Makefile. When I run it it appears the following errors:</p>
  
  <pre><code>compilando main.out
  gfortran -o main.out main.o 
  main.o: In function `MAIN__':
  main_KMC.f90:(.text+0x35): undefined reference to `__search_arguments_MOD_get_db_arg'
  main_KMC.f90:(.text+0x57): undefined reference to `__search_arguments_MOD_get_int_arg'
  main_KMC.f90:(.text+0x6a): undefined reference to `__search_arguments_MOD_get_int_arg'
  main_KMC.f90:(.text+0x80): undefined reference to `__search_arguments_MOD_get_int_arg'
  main_KMC.f90:(.text+0x93): undefined reference to `__search_arguments_MOD_get_int_arg'
  main_KMC.f90:(.text+0xa6): undefined reference to `__search_arguments_MOD_get_int_arg'
  main_KMC.f90:(.text+0x48a): undefined reference to `__geometry_MOD_geometry_compute'
  main_KMC.f90:(.text+0x55b): undefined reference to `__mtfort90_MOD_grnd'
  collect2: error: ld returned 1 exit status
  make: *** [main.out] Error 1
  </code></pre>