Compiling mixed C ++ / C code with Fortran using Visual Studio 2013 and Intel Fortran

I am trying to compile a simple C ++ / Fortran Mixed program but have a linking problem. I am using Visual Studio 2013 Ultimate and Intel Visual Fortran Compiler XE 14. The program is very simple and copied from somewhere on the net. It has one C ++ file and one Fortran file

C ++ file:

// Illustrate passing integer and floating point arrays
// from C++ to Fortran
#include <iostream>
using namespace std;
extern "C"
{
      int __stdcall SUMIT(int *, int*);
      float __stdcall MEAN(float*, int*);
}
int main()
{
      int iA[] = { 3, 5, 6, 7, 2, 3, 4, 5, 11, 7 }, iN = 10, iSum;
      float fpA[] = { 1.2f, 3.f, 44.f, 2.5f, -1.3f, 33.44f, 5.f, 0.3f, -3.6f, 24.1f }, fpMean;
      iSum = SUMIT(iA, &iN);
      cout << "The Sum of iA is:" << iSum << endl;
      fpMean = MEAN(fpA, &iN);
      cout << "The Mean of fpA is:" << fpMean << endl;
      return 0;
}

      

Fortran file:

      INTEGER FUNCTION SUMIT(IA,N) 
      INTEGER IA(1)
      ISUM=0
      DO 50 J=1,N
  50  ISUM=ISUM+IA(J)
      SUMIT=ISUM
      RETURN
      END
  C
      REAL FUNCTION MEAN(RA,N)
      REAL RA(1)
      SUM=0.
      DO 50 J=1,N
  50  SUM=SUM+RA(J)
      IF(N.GT.0) MEAN=SUM/FLOAT(N)
      RETURN
      END 

      

For compilation, I created a Fortran library project that compiles a Fortran file in a static library and another C ++ project with a C ++ file that is set as a startup project and depends on the Fortran library project. I have also included the generated fortran library in the C ++ project linker.

Compiler / linker output:

1>------ Rebuild All started: Project: Lib1, Configuration: Debug Win32 ------
1>Deleting intermediate files and output files for project 'Lib1', configuration 'Debug|Win32'.
1>Compiling with Intel(R) Visual Fortran Compiler XE 14.0.3.202 [IA-32]...
1>Source1.f
1>Creating library...
1>
1>Build log written to  "file://D:\work\Lib1\Debug\BuildLog.htm"
1>Lib1 - 0 error(s), 0 warning(s)
2>------ Rebuild All started: Project: Test1, Configuration: Debug Win32 ------
2>  Source.cpp
2>Source.obj : error LNK2019: unresolved external symbol _SUMIT@8 referenced in function _main
2>Source.obj : error LNK2019: unresolved external symbol _MEAN@8 referenced in function _main
2>D:\work\Lib1\Debug\Test1.exe : fatal error LNK1120: 2 unresolved externals 
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========

      

Does anyone have any idea what I might be doing wrong?

+3


source to share


1 answer


You have declared functions in C with the stdcall attribute. This Fortran compiler, without additional directives or compilation options, does not use this calling convention. Get rid of the __stdcall keywords. There may be other differences as well.



It is best to ensure that your Fortran and C code is compatible using the Fortran 2003 C language compatibility feature - in this case, at a minimum, you will apply the BIND (C) suffix to your Fortran procedure statements by supplying the appropriate binding name using the NAME = specifier of this suffix. There are other steps you can take to make the interface more reliable and portable. There are many examples with the fortran-iso-c-binding tag in the answers to the questions on this site.

+3


source







All Articles