title: Undefined reference to Fortran function in C++
body: <p>I can't seem to figure out why this isn't working.</p>
  
  <pre><code>/* main.cpp */
  #include &lt;stdio.h&gt;
  extern "C"
  {
      int __stdcall inhalf(int *);
  }
  int main()
  {
      int toHalf = 2;
      int halved = inhalf(&amp;toHalf);
      printf("Half of 2 is %d", halved);
      return 0;
  }
  </code></pre>
  
  <p>Ok, that looks good.</p>
  
  <pre><code>$ g++ -c main.cpp
  </code></pre>
  
  <p>No errors. </p>
  
  <pre><code>! functions.f90
  function inhalf(i) result(j)
      integer, intent(in) :: i
      integer             :: j
      j = i/2
  end function inhalf
  </code></pre>
  
  <p>I'm pretty sure that's right.</p>
  
  <pre><code>$ gfortran -c functions.f90
  </code></pre>
  
  <p>So far so good...</p>
  
  <pre><code>$ gcc -o testo main.o functions.o
  main.o:main.cpp:(.text+0x24): undefined reference to `inhalf@4'
  collect2.exe: error: ld returned 1 exit status
  </code></pre>
  
  <p>I've been looking this up for over an hour, but I couldn't find anything that worked for this case. How should I solve this?</p>