首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从C++-CLI调用Fortran

从C++-CLI调用Fortran
EN

Stack Overflow用户
提问于 2013-08-21 23:45:38
回答 1查看 549关注 0票数 0

我有一个Fortran子例程FortranShake和一个C++主函数HandShakingTest.cpp。我正在尝试从CLR C++调用Fortran子例程。

我收到两批错误。让我们称它们为ERROR(1)和ERROR(2)。如果您能帮助我理解为什么会发生这些错误,我将不胜感激。

当我尝试使用以下代码进行编译时: cl /clr HandShakingTest.cpp

我得到以下错误(1):

代码语言:javascript
复制
HandShakingTest.obj : error LNK2028: unresolved token (0A00030A) "extern "C" void __c
ecl FortranShake(int &)" (?FortranShake@@$$J0YAXAAH@Z) referenced in function "int __
lrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@Sys
em@@@Z)
HandShakingTest.obj : error LNK2019: unresolved external symbol "extern "C" void __cd
cl FortranShake(int &)" (?FortranShake@@$$J0YAXAAH@Z) referenced in function "int __c
rcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@Syst
m@@@Z)
HandShakingTest.exe : fatal error LNK1120: 2 unresolved externals

然后,我使用以下命令进行编译:

代码语言:javascript
复制
ifort /c FortranShake.f //Which compiles fine
cl /c /clr HandShakingTest.cpp //compiles fine
cl /o test HandShakingTest.obj FortranShake.obj //ERROR(2) occurs

错误(2)包括:

代码语言:javascript
复制
MSVCRT.lib(ti_inst.obj) : error LNK2005: "private: __thiscall type_info::type_info(cla
ss type_info const &)" (??0type_info@@AAE@ABV0@@Z) already defined in LIBCMT.lib(typin
fo.obj)
MSVCRT.lib(ti_inst.obj) : error LNK2005: "private: class type_info & __thiscall type_i
nfo::operator=(class type_info const &)" (??4type_info@@AAEAAV0@ABV0@@Z) already defin
ed in LIBCMT.lib(typinfo.obj)
MSVCRT.lib(merr.obj) : error LNK2005: __matherr already defined in LIBCMT.lib(_matherr
_.obj)
LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NOD
EFAULTLIB:library
HandShakingTest.obj : error LNK2028: unresolved token (0A00030A) "extern "C" void __cd
ecl FortranShake(int &)" (?FortranShake@@$$J0YAXAAH@Z) referenced in function "int __c
lrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@Syst
em@@@Z)
HandShakingTest.obj : error LNK2019: unresolved external symbol "extern "C" void __cde
cl FortranShake(int &)" (?FortranShake@@$$J0YAXAAH@Z) referenced in function "int __cl
rcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@Syste
m@@@Z)
libifcoremt.lib(for_main.obj) : error LNK2019: unresolved external symbol _MAIN__ refe
renced in function _main
test.exe : fatal error LNK1120: 3 unresolved externals

下面是HandShakingTest.cpp:

代码语言:javascript
复制
#include "stdio.h"
#include <stdlib.h>
#include <Windows.h>
#using <System.DLL>
#using <System.Windows.Forms.DLL>

using namespace std;
using namespace System;
using namespace System::IO;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;

extern "C" {void FortranShake(int&);}

int main(array<System::String ^> ^args)
{
    Process^ testHand = gcnew Process();
    testHand->StartInfo->UseShellExecute = false;
    testHand->StartInfo->RedirectStandardInput = true;
    testHand->StartInfo->RedirectStandardOutput = true;
    testHand->StartInfo->ErrorDialog = true;

    int numDebug = 0;
    String^ returnedDebug = "Nothing";

    FortranShake(numDebug);

    StreamReader^ FromHandProcess = testHand->StandardOutput;
    StreamWriter^ ToHandProcess = testHand->StandardInput;

    String^ Line;

    Line = FromHandProcess ->ReadLine();

    if (Line->Equals("Enter Hand") )
    {
        Console::WriteLine(L"Hand Started!");
    }
    ToHandProcess ->WriteLine(numDebug.ToString());
    returnedDebug = FromHandProcess ->ReadLine();

    MessageBox::Show(returnedDebug);

    return 0;
}

下面是Fortran子例程:

代码语言:javascript
复制
  SUBROUTINE FortranShake(GP_DEBUG)
  IMPLICIT DOUBLE PRECISION (A-H,O-Z)
  INN = 5

  WRITE(06,'(a)') 'Enter Hand'

  READ(INN,*) GP_DEBUG
  GP_DEBUG = GP_DEBUG + 55
  WRITE(06,*) GP_DEBUG

  RETURN
  END
EN

回答 1

Stack Overflow用户

发布于 2013-08-22 05:25:52

您的第一个错误实际上是一个链接器错误-没有/c命令行开关,您将在一个步骤中编译和链接。未提供Fortran代码或目标代码。

您的第二个错误是因为:

  • 您为C++和Fortran指定的运行时库(通过遗漏)不匹配。您需要决定是否要使用静态链接( current的默认设置)(从今天开始,但不一定是从下个月开始...)windows上的英特尔Fortran版本)或动态链接( MS C++编译器的默认设置)。也许将Fortran添加到指定动态linking.
  • Without编译器选项或指令的ifort命令行中。相反,由/MD编译器生成的C代码中的Fortran过程的等效标识符是Fortran过程名称的大写变体-即在C++代码中调用过程FORTRANSHAKE。如果您可以按照F2003标准编写Fortran代码,则应该使用该语言(BIND(C,...))的C互操作性功能来控制Fortran过程的C绑定名称,并确保调用约定等一致。
  • Fortran子例程的伪参数具有双精度类型说明符,对于这种编译器组合,它等同于C++中的double,而不是int。同样,F2003引入了一些功能,可以使这种参数类型对齐更加健壮。
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18362004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档