title: Calling Fortran from C++-CLI
body: <p>I have a fortran subroutine, FortranShake, and a C++ main function, HandShakingTest.cpp.
  I'm trying to call a fortran subroutine from CLR C++.</p>
  
  <p>I'm getting two batches of errors. Let's call them ERROR(1) and ERROR(2). I'd really appreciate if you could help me understand why these errors are happening.</p>
  
  <p>When I try to compile with the following:
      cl /clr HandShakingTest.cpp</p>
  
  <p>I get the following ERROR(1):</p>
  
  <pre><code>HandShakingTest.obj : error LNK2028: unresolved token (0A00030A) "extern "C" void __c
  ecl FortranShake(int &amp;)" (?FortranShake@@$$J0YAXAAH@Z) referenced in function "int __
  lrcall main(cli::array&lt;class System::String ^ &gt;^)" (?main@@$$HYMHP$01AP$AAVString@Sys
  em@@@Z)
  HandShakingTest.obj : error LNK2019: unresolved external symbol "extern "C" void __cd
  cl FortranShake(int &amp;)" (?FortranShake@@$$J0YAXAAH@Z) referenced in function "int __c
  rcall main(cli::array&lt;class System::String ^ &gt;^)" (?main@@$$HYMHP$01AP$AAVString@Syst
  m@@@Z)
  HandShakingTest.exe : fatal error LNK1120: 2 unresolved externals
  </code></pre>
  
  <p>I then used the following commands to compile instead:</p>
  
  <pre><code>ifort /c FortranShake.f //Which compiles fine
  cl /c /clr HandShakingTest.cpp //compiles fine
  cl /o test HandShakingTest.obj FortranShake.obj //ERROR(2) occurs
  </code></pre>
  
  <p>ERROR(2) consists of:</p>
  
  <pre><code>MSVCRT.lib(ti_inst.obj) : error LNK2005: "private: __thiscall type_info::type_info(cla
  ss type_info const &amp;)" (??0type_info@@AAE@ABV0@@Z) already defined in LIBCMT.lib(typin
  fo.obj)
  MSVCRT.lib(ti_inst.obj) : error LNK2005: "private: class type_info &amp; __thiscall type_i
  nfo::operator=(class type_info const &amp;)" (??4type_info@@AAEAAV0@ABV0@@Z) already defin
  ed in LIBCMT.lib(typinfo.obj)
  MSVCRT.lib(merr.obj) : error LNK2005: __matherr already defined in LIBCMT.lib(_matherr
  _.obj)
  LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NOD
  EFAULTLIB:library
  HandShakingTest.obj : error LNK2028: unresolved token (0A00030A) "extern "C" void __cd
  ecl FortranShake(int &amp;)" (?FortranShake@@$$J0YAXAAH@Z) referenced in function "int __c
  lrcall main(cli::array&lt;class System::String ^ &gt;^)" (?main@@$$HYMHP$01AP$AAVString@Syst
  em@@@Z)
  HandShakingTest.obj : error LNK2019: unresolved external symbol "extern "C" void __cde
  cl FortranShake(int &amp;)" (?FortranShake@@$$J0YAXAAH@Z) referenced in function "int __cl
  rcall main(cli::array&lt;class System::String ^ &gt;^)" (?main@@$$HYMHP$01AP$AAVString@Syste
  m@@@Z)
  libifcoremt.lib(for_main.obj) : error LNK2019: unresolved external symbol _MAIN__ refe
  renced in function _main
  test.exe : fatal error LNK1120: 3 unresolved externals
  </code></pre>
  
  <p>Here's the HandShakingTest.cpp:</p>
  
  <pre><code>#include "stdio.h"
  #include &lt;stdlib.h&gt;
  #include &lt;Windows.h&gt;
  #using &lt;System.DLL&gt;
  #using &lt;System.Windows.Forms.DLL&gt;
  
  using namespace std;
  using namespace System;
  using namespace System::IO;
  using namespace System::Diagnostics;
  using namespace System::ComponentModel;
  using namespace System::Collections;
  using namespace System::Windows::Forms;
  
  extern "C" {void FortranShake(int&amp;);}
  
  int main(array&lt;System::String ^&gt; ^args)
  {
      Process^ testHand = gcnew Process();
      testHand-&gt;StartInfo-&gt;UseShellExecute = false;
      testHand-&gt;StartInfo-&gt;RedirectStandardInput = true;
      testHand-&gt;StartInfo-&gt;RedirectStandardOutput = true;
      testHand-&gt;StartInfo-&gt;ErrorDialog = true;
  
      int numDebug = 0;
      String^ returnedDebug = "Nothing";
  
      FortranShake(numDebug);
  
      StreamReader^ FromHandProcess = testHand-&gt;StandardOutput;
      StreamWriter^ ToHandProcess = testHand-&gt;StandardInput;
  
      String^ Line;
  
      Line = FromHandProcess -&gt;ReadLine();
  
      if (Line-&gt;Equals("Enter Hand") )
      {
          Console::WriteLine(L"Hand Started!");
      }
      ToHandProcess -&gt;WriteLine(numDebug.ToString());
      returnedDebug = FromHandProcess -&gt;ReadLine();
  
      MessageBox::Show(returnedDebug);
  
      return 0;
  }
  </code></pre>
  
  <p>Here is the Fortran Subroutine:</p>
  
  <pre><code>  SUBROUTINE FortranShake(GP_DEBUG)
    IMPLICIT DOUBLE PRECISION (A-H,O-Z)
    INN = 5
  
    WRITE(06,'(a)') 'Enter Hand'
  
    READ(INN,*) GP_DEBUG
    GP_DEBUG = GP_DEBUG + 55
    WRITE(06,*) GP_DEBUG
  
    RETURN
    END
  </code></pre>