title: FORTRAN Makefile for a large project
body: <p>I am starting a new project in Fortran95 that should become big quickly (a lot of modules). At the moment I have only</p>
  
  <ul>
  <li>output.f90</li>
  <li>moldyn.f90</li>
  </ul>
  
  <p>output.f90 contains a module with a few public variables, two public subroutines and two private subroutines. moldyn.f90 contains the main program calling the two public subroutines contained in output.f90.</p>
  
  <p>It is the first time that I tackle a large project, therefore I would like to automate as much as possible compilation and linking by creating a general and easily adaptable Makefile. Makefiles I created before were only "hard-coded" compilation and linking statements and for a large project I would like to automate the process (but without using other tools as CMake).</p>
  
  <p>This is my attempt that unfortunately does not work:</p>
  
  <pre><code># ========
  # Makefile
  # ========
  
  # Makefile name
  MF = Makefile
  
  # ==========
  # Executable
  # ==========
  
  # Executable name
  EXE = $(BINDIR)/moldyn.x
  
  # ========
  # Compiler
  # ========
  
  # Fortran compiler
  FC = gfortran
  
  # Fortran flags
  FFLAGS = -O2 -Wall -Warray-bounds -fbounds-check
  
  # ===========
  # Directories
  # ===========
  SRCDIR = src
  BINDIR = bin
  OBJDIR = obj
  MODDIR = mod
  
  # ========
  # Suffixes
  # ========
  
  .SUFFIXES:
  .SUFFIXES: .f90 .o .mod
  
  # ===================
  # Sources and objects
  # ===================
  
  SRC = \
      $(SRCDIR)/moldyn.f90\
      $(SRCDIR)/output.f90
  
  OBJ = $(SRC:$(SRCDIR)%.f90=$(OBJDIR)%.o)
  
  # =====
  # Rules
  # =====
  
  all:
      @echo $(SRC)
      @echo $(OBJ)
  
  moldyn: $(OBJDIR)/.o $(EXE)
  
  $(EXE): $(OBJ)
      $(FC) $(FFLAGS) -o $(SRCDIR)/$@ $(OBJ)
  
  $(OBJDIR)/.o:
      $(FC) $(FFLAGS) -c $(SRC)
  
  $(OBJ): $(MF)
  
  # =======
  # Folders
  # =======
  
  $(BINDIR):
      mkdir -p $(BINDIR)
  
  $(OBJDIR):
      mkdir -p $(OBJDIR)
  
  $(MODDIR):
      mkdir -p $(MODDIR)
  
  # =====
  # Clean
  # =====
  
  clean:
      rm -f src/*~
      rm -fr $(BINDIR) $(OBJDIR) $(MODDIR)
  </code></pre>
  
  <p>I think that the mess comes from the fact that I have the sources in a src/ folder and I want to create the .o in an obj/ folder and the executable in bin/ folder. .mod files also play a role since they are created in the dase folder (where there is the makefile), but I don't really know how to deal with them (it is my first Fortran project).</p>
  
  <p>Can anyone help me to understand where compilations problems come from? The error I get is the following:</p>
  
  <pre><code>gfortran -O2 -Wall -Warray-bounds -fbounds-check -c src/moldyn.f90 
  
  src/output.f90
  src/moldyn.f90:20.6:
  
    use output, only: title, gpl
        1
  Fatal Error: Can't open module file 'output.mod' for reading at (1): No such file or directory
  make: *** [obj/.o] Error 1
  </code></pre>
  
  <p>and seems to be related with the .mod files.</p>