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

故事扮演 提交于 2020-01-03 19:43:52

问题


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

The C++ file is:

// 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;
}

The fortran file is:

      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 

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

The output of the compilers/linker is:

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 anybody have an idea of what I might be doing wrong?


回答1:


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

A far better way of ensuring that your Fortran and C code is interoperable is to use the Fortran 2003 language C interoperability feature - in this case as a minimum yaou would apply the BIND(C) suffix to your Fortran procedure declarations, supplying an appropriate binding name via the NAME= specifier of that suffix. There are other steps you can take to further make the interface more robust and portable. There are many examples in answers to questions on this site with the fortran-iso-c-binding tag.



来源:https://stackoverflow.com/questions/25384590/compiling-mixed-c-c-code-with-fortran-using-visual-studio-2013-and-intel-fortr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!