我正在尝试编译一个简单的 C++/Fortran 混合程序,但存在链接问题。我使用的是Visual Studio 2013 Ultimate和Intel Visual Fortran Compiler XE 14。该程序非常简单,是从网上的某个地方复制的。它有 1 个 C++ 文件和 1 个 Fortran 文件
C++ 文件是:
// 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 文件是:
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
为了编译,我创建了一个 Fortran 库项目,用于编译静态库中的 Fortran 文件,以及另一个 C++ 项目,其中 C++ 文件设置为启动项目并依赖于 Fortran 库项目。我还将生成的 Fortran 库包含在 C++ 项目链接器中。
编译器/链接器的输出是:
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 ==========
有人知道我可能做错了什么吗?