title: How to use fortran module subroutine in c
body: <p>I'm trying to use a fortran module subroutine in c and cannot get through, here is a simplified version of my problem:</p>
  
  <p>I have one fortran module that contains a subroutine, and the second subroutine uses the module.</p>
  
  <pre><code>!md.f90
  module myadd
  implicit none
  contains
  
  subroutine add1(a) bind(c)
  implicit none
  integer a
  a=a+1
  
  end subroutine add1
  end module myadd
  
  
  !sb.f90
  subroutine sq(a) bind(c)
  use myadd
  implicit none
  integer a
  call add1(a)
  a=a*a
  end subroutine sq
  </code></pre>
  
  <p>Now I want to call the <code>sb</code> function in c:</p>
  
  <pre><code>//main.cpp
  extern "C"{ void sb(int * a); }
  int main(){
    int a=2;
    sb(&amp;a);
  }
  </code></pre>
  
  <p>how should I link them together?</p>
  
  <p>I tried something like</p>
  
  <pre><code>ifort -c md.f90 sb.f90
  icc sb.o main.cpp
  </code></pre>
  
  <p>but it gives error</p>
  
  <blockquote>
    <p>sb.o: In function <code>sq': sb.f90:(.text+0x6): undefined reference to
    </code>add1' /tmp/icc40D9n7.o: In function <code>main': main.cpp:(.text+0x2e):
    undefined reference to</code>sb'</p>
  </blockquote>
  
  <p>Does anyone know how to solve the problem?</p>