title: C++-Fortran mixcompile unresolved externals
body: <p>I am trying to make a fortran-C++ mix compile project. I use the case to test how the visual studio 2017 compiler link the intel fortran .lib file. Here is the example code:</p>
  <p>Fortran code here:</p>
  <pre><code>!DEC$ IF DEFINED (_DLL)
  !DEC$ ATTRIBUTES DLLEXPORT :: ADD
  !DEC$ END IF
  REAL*8 FUNCTION ADD (A, B, C, D) 
  REAL*8 B,D
  INTEGER*4 A,C
  DIMENSION B(4), D(4)
  ADD = B(A) + D(C)
  RETURN
  END
  </code></pre>
  <p>C++ code here:</p>
  <pre><code>#include &lt;iostream&gt;
  extern &quot;C&quot; {double add_(int *, double[], int *, double[]); }
  
  double ar1[4] = { 1.0, 2.0, 3.0, 4.0 };
  double ar2[4] = { 5.0, 6.0, 7.0, 8.0 };
  
  int main()
  {
      int x, y;
      double z;
  
      x = 3;
      y = 3;
  
  
      z = add_(&amp;x, ar1, &amp;y, ar2); /* Call Fortran add routine */
      /* Note: Fortran indexes arrays 1..n */
      /* C indexes arrays 0..(n-1) */
  
      std::cout &lt;&lt; z &lt;&lt; std::endl;
      return 0;
  }
  </code></pre>
  <p>The project setup procedures are:</p>
  <ol>
  <li>Open VS studio, create a c++ empty project, add the c++ code in the source file. Solution will be automatically created as well.</li>
  <li>Add a new fortran static library project in the solution. Add the fortran code in the source file.</li>
  <li>Fortran project-&gt; properties-&gt;Libraries-&gt;Runtime Libraries-&gt; Debug multithread dll.</li>
  <li>Build the fortran project, find the .lib file directory.</li>
  <li>C++ project-&gt; linker -&gt; General -&gt; Additional Library Dependencies -&gt; add the .lib file directory here</li>
  <li>C++ project-&gt; linker -&gt; Input -&gt; Additional dependencies -&gt; Add .lib file name here end with &quot;;&quot;.</li>
  <li>Build C++ project.</li>
  </ol>
  <p>And the compiler error message is:</p>
  <pre><code>1&gt;------ Build started: Project: Cpp_main, Configuration: Debug Win32 ------
  1&gt;Source.obj : error LNK2019: unresolved external symbol _add_ referenced in function _main
  1&gt;C:\Users\Documents\TEST_Project\MixCompile_Test\Mix_compile\Debug\Cpp_main.exe : fatal error LNK1120: 1 unresolved externals
  1&gt;Done building project &quot;Cpp_main.vcxproj&quot; -- FAILED.
  ========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
  </code></pre>
  <p>Why the compiler cannot resolve the external function call even if I assign the .lib directory in C++ project?</p>
  <p>Thanks a lot!</p>
  <hr />
  <p>Update 22-6-24:
  Thank you Steve. I followed your instruction and made configuration in VC++ directories to add the ifortran include and lib directory into it. But new problem happened:</p>
  <pre><code>1&gt;------ Build started: Project: Cpp_main, Configuration: Debug Win32 ------
  1&gt;LINK : fatal error LNK1104: cannot open file 'msvcprtd.lib'
  1&gt;Done building project &quot;Cpp_main.vcxproj&quot; -- FAILED.
  ========== Build: 0 succeeded, 1 failed, 2 up-to-date, 0 skipped ==========
  
  </code></pre>
  <hr />
  <p>Update 22-06-24
  I solved the issue. I will upload the whole solution tomorrow. It's really special  problem for VS2017.</p>
  <p>Fortran compiler: Inter ifortran19
  Cpp compiler: MSVC 2017</p>
  <p>Step:</p>
  <ol>
  <li><p>Create your CPP project and solution.</p>
  </li>
  <li><p>In the solution, add a fortran static library project with name .</p>
  </li>
  <li><p>Add your fortran headfile and source code in that project.</p>
  </li>
  <li><p>For every fortran subroutine that will be called in CPP, do like this:</p>
  <p>subroutine_name() BIND(C)
  USE, INTRINSIC :: ISO_C_BINDING<br />
  
  end subroutine</p>
  </li>
  <li><p>For CPP headfile, if the fortran compiler is interl fortran, do not add underscore_ after the function name!! Just declare the function name with extern &quot;C&quot; like this:
  extern &quot;C&quot;{void &lt;fortran_subroutine&gt;()}
  Remember, all the arguments passed from C code should be reference or pointer. Do not pass value.</p>
  </li>
  <li><p>In the C compiler, add several dependency directories:</p>
  <p>6.1 C project properties-&gt; VC++ Directories -&gt; Include Directries -&gt; add $(IFORT_COMPILER19)\compiler\include;</p>
  <p>6.2 C project properties-&gt; VC++ Directories -&gt; Libiary Directries -&gt; add $(IFORT_COMPILER19)\compiler\lib; (+\ia32_win or +\intel64_win depend on win32 or X64 compiler used in VS)</p>
  </li>
  <li><p>In the C linker, add several dependency directories:</p>
  <p>7.1 C project properties-&gt; linker-&gt; General-&gt; Additional Dependency Directories-&gt;
  add  C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\lib
  (+\x86 or +\x64 depend on win32 or x64 compiler used in VS)</p>
  <p>7.2 C project properties-&gt; linker-&gt; General-&gt; Additional Dependency Directories-&gt;
  add the fortran static library compiled  file directory</p>
  <p>7.3 C project properties-&gt; linker-&gt;input add .lib</p>
  </li>
  <li><p>In the source code, include the header file with extern “C” keyword declaration and call the function
  &lt;fortran_subroutine&gt;()</p>
  </li>
  </ol>