title: Linking fortran module: "undefined reference"
body: <p>I'm trying to write some functions/subroutines in a module that call another function in the same module and running into linker errors. A toy example displaying the same behavior:</p>
  
  <pre><code>!in test.f
  
  module m1
    implicit none
  contains
    real function mult(a, b)
      real :: a
      real :: b
      mult = a * b
      return
    end function mult
  
    real function sq(a)
      real :: a, mult
      sq = mult(a, a)
      return
    end function sq
  
  end module m1
  
  program main
  use m1
  write(*,*) sq(2.0)
  end program
  </code></pre>
  
  <p>When I try to compile this, I run into trouble:</p>
  
  <pre><code>[christopher@archlinux metropolis]$ gfortran -ffree-form test.f
  /tmp/ccpzdTLE.o: In function `__m1_MOD_sq':
  test.f:(.text+0x20): undefined reference to `mult_'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>On the other hand, compiling only (<code>gfortran -c -ffree-form test.f -Wall</code>) runs with no complaint.</p>
  
  <p>Now this looks for all the world like a compiler error---in the module it comes up with a reference to <code>mult_</code> when it really ought to com up with <code>__m1_MOD_sq</code>---but I have a very hard time believing that this is a compiler bug rather than me doing something stupid.</p>
  
  <p>DDG didn't turn up anything useful. Most of the similar problems ocurred in splitting the module off from one main file. In those cases, things worked when the module was in the same file as the <code>program</code>, which is not the case here. I looked at a number of pages on modules in Fortran and didn't see anything relevant.</p>
  
  <p>Can anyone point me to appropriate documentation or, better yet, explain what's going on and how I can fix it?</p>