问题描述
我需要在C ++代码中调用Fortran程序.该程序使用Intel编译器进行编译.Fortran程序跨越多个文件,以前被称为PROGRAM.我试图做的是将主PROGRAM调用更改为一个名为SIMULATOR的SUBROUTINE.然后,我将每个Fortran源代码编译成一个目标文件,而没有将它们全部放入可执行文件中.然后,我准备用一个简单的C ++包装器将所有Fortran对象链接起来进行测试.代码和makefile紧随其后.
I need to call a Fortran program within a C++ code. The program is compiled using an Intel compiler The Fortran program spans several files, and used to be called as a PROGRAM. What I tried to do was change the main PROGRAM call to a SUBROUTINE called SIMULATOR. Then, I compiled each Fortran source code into an object file without putting them all into an executable. I then got ready to link all the Fortran objects up with a simple C++ wrapper for testing. The code and makefile follow.
wrapper.cpp:
wrapper.cpp:
#include <iostream.h>
using namespace std;
extern "C"{
void simulator_();
}
int main()
{
cout << "starting..." << endl;
simulator_();
cout << "success!" << endl;
return 0;
}
Makefile:
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是包含我要调用的模拟器功能的文件:
Here is (part of) the output from the compiler. Simsys is the file that contains the simulator function I'm trying to call:
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,因此,我有点想着从这里开始.
Now according to a person with a similar problem (http://www.velocityreviews.com/forums/t288905-intel-compiler-8-1-c-calling-fortran-routine.html), it seems like I'm missing some libraries. That person wrote that they included a specific library and it helped them, but I don't know how to begin to look for what libraries I need. I also don't know how to find the path of the intel compiler on the HPC system I'm using, so I would appreciate some help looking in the right direction. I didn't have to do anything special to compile the fortran PROGRAM before I was trying to link it with the C++, so I'm kind of stuck in thinking about where to go from here.
顺便说一句,Fortran程序不需要任何输入,它们都是独立的.
By the way, the Fortran program doesn't need any inputs, it's all self-contained.
在此先感谢您的帮助和见解!
Thanks in advance for your help and insights!
哪个ifort给了我:/usr/global/intel/Compiler/11.1/073/bin/intel64/ifort
which ifort gave me: /usr/global/intel/Compiler/11.1/073/bin/intel64/ifort
尝试使用ifort进行最终链接后,出现以下错误:
After trying to do the final link using ifort, I got the following error:
ld:找不到-lunwind
ld: cannot find -lunwind
我找到了有关展开的文档(https://savannah.nongnu.org/news/?group=libunwind),但我没有尝试自己调用此库,也不知道它来自何处.
I found documentation about unwind (https://savannah.nongnu.org/news/?group=libunwind) but I didn't try to call this library myself and don't know where this was coming from.
推荐答案
最简单的方法是使用ifort进行链接,以获得Fortran运行时库.某些说明位于 http://www.ualberta.ca/AICT/RESEARCH/LinuxClusters/doc/ifc91/main_for/mergedProjects/bldaps_for/pgsclmix.htm
The easy way is to use ifort for the linking, to obtain the Fortran runtime libraries. Some instructions are at 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绑定将变得更加有用.
If you use the ISO_C_Binding on the Fortran side you can take control of the name of the routine and get rid of the underscore. Trivial ... the ISO C Binding will become much more useful if you start adding arguments between C/C++ and Fortran.
这篇关于使用Intel编译器从C ++调用Fortran子例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!