使用英特尔编译器从C ++调用Fortran子例程

时间:2012-04-22 22:33:27

标签: c++ fortran

我需要在C ++代码中调用Fortran程序。该程序使用英特尔编译器编译.Fortran程序跨越多个文件,以前称为程序。我试图做的是将主程序调用更改为名为SIMULATOR的SUBROUTINE。然后,我将每个Fortran源代码编译成一个目标文件,而不是将它们全部放入可执行文件中。然后我准备将所有Fortran对象与一个简单的C ++包装器连接起来进行测试。代码和makefile如下。

wrapper.cpp:

#include <iostream.h>

using namespace std;

extern "C"{
void simulator_();
}

int main()
{
    cout << "starting..." << endl;
    simulator_();
    cout << "success!" << endl;
    return 0;
}

生成文件:

all:
    ifort -nologo -O3 -cxxlib -nofor-main -gen-interfaces -traceback -check bounds -save -static -threads -c modules.for
    ifort -nologo -O3 -cxxlib -nofor-main -gen-interfaces -traceback -check bounds -save -static -threads -c Finterp.for
--a few more sources go here--
    ifort -nologo -O3 -cxxlib -nofor-main -gen-interfaces -traceback -check bounds -save -static -threads -c Simsys.for
    icpc -c wrapper.cpp
    icpc -o cppprogram *.o

这是编译器输出的(部分)。 Simsys是包含我试图调用的模拟器函数的文件:

Simsys.o: In function `simsys_':
Simsys.for:(.text+0xed4): undefined reference to `for_write_int_lis'
Simsys.for:(.text+0xeef): undefined reference to `for_adjustl'
Simsys.for:(.text+0xf38): undefined reference to `for_trim'
Simsys.for:(.text+0xf83): undefined reference to `for_concat'
Simsys.for:(.text+0x1071): undefined reference to `for_open'
Simsys.for:(.text+0x131d): undefined reference to `for_emit_diagnostic'
Simsys.o:Simsys.for:(.text+0x1670): more undefined references to `for_emit_diagnostic' follow

现在根据一个有类似问题的人(http://www.velocityreviews.com/forums/t288905-intel-compiler-8-1-c-calling-fortran-routine.html),看起来好像我我错过了一些图书馆。那个人写道,他们包括一个特定的库,它帮助了他们,但我不知道如何开始寻找我需要的库。我也不知道如何在我正在使用的HPC系统上找到intel编译器的路径,所以我希望看到一些正确的方向帮助。在尝试将它与C ++链接之前,我没有做任何特殊的编译fortran PROGRAM的事情,所以我有点想到从这里去的地方。

顺便说一句,Fortran程序不需要任何输入,它都是独立的。

提前感谢您的帮助和见解!

编辑:

ifort给了我:     /usr/global/intel/Compiler/11.1/073/bin/intel64/ifort

尝试使用ifort进行最终链接后,出现以下错误:

ld:找不到-lunwind

我找到了关于放松的文档(https://savannah.nongnu.org/news/?group=libunwind),但我没有尝试自己调用这个库,也不知道它来自哪里。

2 个答案:

答案 0 :(得分:3)

简单的方法是使用ifort进行链接,以获取Fortran运行时库。一些说明位于http://www.ualberta.ca/AICT/RESEARCH/LinuxClusters/doc/ifc91/main_for/mergedProjects/bldaps_for/pgsclmix.htm

如果在Fortran端使用ISO_C_Binding,则可以控制例程的名称并删除下划线。如果你开始在C / C ++和Fortran之间添加参数,那么ISO C Binding将变得更加有用。

答案 1 :(得分:2)

编译/链接fortran和C ++是非常可行的,但有一些细节需要解决。英特尔帮助他们在线提供文档: http://software.intel.com/en-us/articles/intel-fortran-composer-xe-documentation/

请参阅“英特尔Fortran编译器用户/参考指南”,特别是“编译器参考”/“混合语言编程”/“Fortran / C混合语言程序”部分。 “编译器参考”/“库”部分也很有用。

最后,您需要在链接行中包含一些fortran静态库,您可能需要在应用程序中重新分配一些fortran运行时dll。

相关问题