使用Visual Studio 2013和英特尔Fortran编译Fortran混合C ++ / C代码 [英] Compiling mixed C++/C code with Fortran using Visual Studio 2013 and Intel Fortran

查看:939
本文介绍了使用Visual Studio 2013和英特尔Fortran编译Fortran混合C ++ / C代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编译一个简单的C ++ / Fortran混合程序,但有链接问题。我正在使用Visual Studio 2013 Ultimate和Intel Visual Fortran编译器XE 14.该程序非常简单,并且从网络的某处复制。它有一个C ++文件和一个Fortran文件



C ++文件是:

  //说明传递整数和浮点数组
//从C ++到Fortran
#include >
使用namespace std;
externC
{
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<< iA的总和是:<< iSum<< ENDL;
fpMean = MEAN(fpA,& iN);
cout<< fpA的平均值是:<< fpMean<< ENDL;
返回0;
}

fortran文件是:

  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
实际功能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

为了编译,我创建了一个Fortran库项目,它在静态库中编译Fortran文件另一个C ++项目是C ++文件,它被设置为启动项目并依赖于Fortran库项目。我还将生成的fortran库包含在C ++项目链接器中。



编译器/链接器的输出为:

  1> ------ Rebuild All started:Project:Lib1,Configuration:Debug Win32 ------ 
1>删除中间文件和输出文件对于项目'Lib1',配置'Debug | Win32'。
1>使用英特尔(R)Visual Fortran编译器XE编译14.0.3.202 [IA-32] ...
1> Source1.f
1>创建库...
1>
1>构建日志写入file:// D:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' )
2> ------重建全部开始:Project:Test1,Configuration:Debug Win32 ------
2> Source.cpp
2> Source.obj:error LNK2019:无法解析的外部符号_SUMIT @ 8在函数中引用_main
2> Source.obj:错误LNK2019:无法解析的外部符号_MEAN @ 8在函数_main中引用
2> D:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\失败,0跳过==========

有人知道什么是什么我可能做错了?

解决方案

您已经使用stdcall属性在C中声明了函数。 Fortran编译器不使用其他指令或编译选项,不使用该调用约定。摆脱__stdcall关键字。可能还有其他差异。

确保Fortran和C代码可互操作的更好方法是使用Fortran 2003语言C互操作性功能 - 在此至少你会将BIND(C)后缀应用于Fortran过程声明,并通过该后缀的NAME =说明符提供适当的绑定名称。您还可以采取其他步骤来进一步提高界面的可靠性和便携性。使用fortran-iso-c-binding标签,本网站上有许多问题的答案。


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?

解决方案

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.

这篇关于使用Visual Studio 2013和英特尔Fortran编译Fortran混合C ++ / C代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆