title: Mixed programming: Calling FORTRAN from C++
body: <p>I am trying to compile a mixed program with command line.</p>
  
  <p>Compile tool is intel visual fortran 2013 and VS 2013.</p>
  
  <p>I have googled around and found the using <code>dll</code> is easier than <code>lib</code>.</p>
  
  <p>So I tried to compile my fortran  source as a dll file</p>
  
  <p>my <code>Fortran</code> source:</p>
  
  <pre><code>!  forsubs.f90 
  !
  !  FUNCTIONS/SUBROUTINES exported from FORSUBS.dll:
  ! FORSUBS      - subroutine 
  !
  INTEGER*4 FUNCTION Fact (n)
     !DEC$ ATTRIBUTES DLLEXPORT::Fact
        INTEGER*4 n [VALUE]
        INTEGER*4 i, amt
        amt = 1
        DO i = 1, n
          amt = amt * i
        END DO
        Fact = amt
     write(*,*)"Mixed calls succeed!"
  END
  SUBROUTINE Pythagoras (a, b, c)
     !DEC$ ATTRIBUTES DLLEXPORT::Pythagoras
        REAL*4 a [VALUE]
        REAL*4 b [VALUE]
        REAL*4 c [REFERENCE]
        c = SQRT (a * a + b * b)
  END
  </code></pre>
  
  <p>my <code>C++</code> source: c_main.cpp</p>
  
  <pre><code>/*     File CMAIN.C   */
  
  #include &lt;stdio.h&gt;
  
  extern int __stdcall fact(int* n);
  extern void __stdcall pythagoras(float* a, float* b, float *c);
  
  int main()
  {
      float c;
      printf("Factorial of 7 is: %d\n", FACT(7));
      PYTHAGORAS (30, 40, &amp;c);
      printf("Hypotenuse if sides 30, 40 is: %f\n", c);
  }
  </code></pre>
  
  <h3>First I compiled my fortran source:</h3>
  
  <blockquote>
    <p>ifort /dll forsubs.f90</p>
  </blockquote>
  
  <p>and got a <code>forsubs.dll</code> and a <code>forsubs.lib</code>.</p>
  
  <h3>Second I compiled my C++ source:</h3>
  
  <blockquote>
    <p>cl c_main.cpp /link forsubs.lib</p>
  </blockquote>
  
  <p>However, I got an error:</p>
  
  <pre><code>D:\c_f\c_f_dll\c_main.cpp(11) : error C3861: “FACT”:  identifier not found
  D:\c_f\c_f_dll\c_main.cpp(12) : error C3861: “PYTHAGORAS”:  identifier not found
  </code></pre>
  
  <p>Also, I have tried to load dll in another way, by using:</p>
  
  <pre><code> HINSTANCE hLibrary=LoadLibrary("forsubs.dll"); 
   if(hLibrary==NULL)
   {
    cout&lt;&lt;"can't find the dll file"&lt;&lt;endl;
    return -1;
   }
  </code></pre>
  
  <p>But I got an "can't find the dll file" everytime while I am sure the <code>forsubs.dll</code> is in the folder.</p>
  
  <p>I am not good at C++ and can not find out how to compile it, help!</p>
  
  <h2>Edit</h2>
  
  <p>Strange! I edited my program as @Mikael Persson 's answer, but still got an error.</p>
  
  <pre><code>error LNK2019: unresolved external symbol _FACT@4 referenced in function _main
  error LNK2019: unresolved external symbol _PYTHAGORAS@12 referenced in function _main
  </code></pre>
  
  <p>Also, I googled around and someone said <code>__stdcall</code> is not nessary ,I delete but still got error:</p>
  
  <pre><code>error LNK2019: unresolved external symbol _FACT referenced in function _main
  error LNK2019: unresolved external symbol _PYTHAGORAS referenced in function _main
  </code></pre>
  
  <p>I thought it may be the same problem like <a href="https://stackoverflow.com/questions/23669364/mixed-programming-calling-fortran-from-c">this</a>. But can not solve it by myself.</p>
  
  <p>PS:
  info of my <code>dll</code> and <code>lib</code>:</p>
  
  <pre><code>D:\c_f\c_f_dll&gt;dumpbin /exports forsubs.lib
  Microsoft (R) COFF/PE Dumper Version 12.00.30501.0
  Copyright (C) Microsoft Corporation.  All rights reserved.
  
  
  Dump of file forsubs.lib
  
  File Type: LIBRARY
  
       Exports
  
         ordinal    name
  
                    FACT
                    PYTHAGORAS
  
    Summary
  
            C3 .debug$S
            14 .idata$2
            14 .idata$3
             8 .idata$4
             8 .idata$5
             C .idata$6
  
  D:\c_f\c_f_dll&gt;dumpbin /exports forsubs.dll
  Microsoft (R) COFF/PE Dumper Version 12.00.30501.0
  Copyright (C) Microsoft Corporation.  All rights reserved.
  
  
  Dump of file forsubs.dll
  
  File Type: DLL
  
    Section contains the following exports for forsubs.dll
  
      00000000 characteristics
      5477F1C5 time date stamp Fri Nov 28 11:53:41 2014
          0.00 version
             1 ordinal base
             2 number of functions
             2 number of names
  
      ordinal hint RVA      name
  
            1    0 00001000 FACT
            2    1 000010B0 PYTHAGORAS
  
    Summary
  
          1000 .data
          1000 .pdata
          1000 .rdata
          1000 .reloc
          1000 .text
  </code></pre>
  
  <h2>Edit2</h2>
  
  <pre><code>Also, I have tried to add a header:
  #ifdef FORSUBS_EXPORTS
  #define FORSUBS_API __declspec(dllexport) 
  #else
  #define FORSUBS_API __declspec(dllimport) 
  #endif
  </code></pre>
  
  <p>it still not work.</p>