title: Calling C++ from Fortran (linking issue?)
body: <p>I really need your help! I'm on a deadline and I'm trying to learn just enough to get some work done. It's been well over a week now that I'm dealing with what appears to be a straightforward issue but I haven't been able to successfully implement solutions online.</p>
  
  <p>Long story short: I need to call C++ code from F77. I'm compiling with g++ and gfortran. I'm a complete newb to makefiles. When these codes are compiled as part of their respective programs, they are bug free (I'm taking a function from my C++ code, not main(), and trying to use it with the fortran code). Here's what I've got:</p>
  
  <p><strong>C++ Code:</strong></p>
  
  <pre><code>#include &lt;cmath&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  extern"C" double ShtObFun(double x[], int &amp;tp)
  {
      return //double precision awesomeness
  }
  </code></pre>
  
  <p><strong>Fortran Code:</strong></p>
  
  <pre><code>    subroutine objfun(nv, var, f, impass)
      implicit real(8) (a-h,o-z), integer (i-n)
  c   initializations including tp, used below
  
      f = ShtObFun(var, tp)
  
      return
      end
  </code></pre>
  
  <p><strong>Makefile (shows only files listed above):</strong></p>
  
  <pre><code>all:
      g++ -c Objective_Functions.cpp
      gfortran -c -O3 opcase1.f
      gfortran opcase1.o Objective_Functions.o -fbounds-check -lstdc++ -g -o Program.out
      rm *.o
  </code></pre>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>opcase1.o: In function 'objfun_':
  opcase1.f:(.text+0xbd): undefined reference to 'shtobfun_'
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>I have tried this a variety of other ways and they did not work. I can list those later if requested. Does anyone see the issue here?</p>
  
  <p>The sites I've checked:</p>
  
  <p><a href="https://stackoverflow.com/questions/7255468/calling-c-function-from-fortran-not-c">calling C++ function from fortran not C</a>, 
  <a href="https://stackoverflow.com/questions/5663083/linking-fortran-and-c-binaries-using-gcc">Linking fortran and c++ binaries using gcc</a>, , <a href="https://stackoverflow.com/questions/354182/calling-c-code-from-fortran">Calling C Code from FORTRAN</a>, <a href="http://star-www.rl.ac.uk/docs/sun209.htx/node4.html" rel="nofollow noreferrer">Cookbook - Calling C from Fortran</a>, <a href="http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html" rel="nofollow noreferrer">YoLinux - Using C/C++ and Fortran together</a></p>
  
  <p><strong>Edit (response to first answer):</strong></p>
  
  <p>If I rewrite C++ code as:</p>
  
  <pre><code>#include &lt;cmath&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  double ShtObFun(double x[], int &amp;tp)
  extern"C" double shtobfun_(double *x, int *tp) {
      return ShtObFun(x, *tp);
  }
  {
      cout &lt;&lt; "reached tp = " &lt;&lt; tp &lt;&lt; endl;
      exit(1);
  }
  </code></pre>
  
  <p>I get this error:
      error: expected initializer before 'extern'
      error: expected unqualified-id before '{' token</p>
  
  <p>If I rewrite C++ code as:</p>
  
  <pre><code>#include &lt;cmath&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  double ShtObFun(double x[], int &amp;tp);
  
  extern"C" double shtobfun_(double *x, int *tp) {
      return ShtObFun(x, *tp);
  }
  
  double ShtObFun(double x[], int &amp;tp)
  {
      cout &lt;&lt; "reached tp = " &lt;&lt; tp &lt;&lt; endl;
      exit(1);
  }
  </code></pre>
  
  <p>The code will compile but the result I get is "reached tp = 0", while it should say "reached tp = 1" because I initialized tp to 1 in the fortran code (integer tp = 1). And I get the same issue if I simply declare the function as:</p>
  
  <pre><code>extern"C" double shtobfun_(double *x, int *tp)
  {
       //cout, etc
  }
  </code></pre>