从fortran调用C ++函数而不是C [英] calling C++ function from fortran not C
问题描述
是否可以从FORTRAN调用一个C ++函数,比如
#include< iostream.h>
externC
{
void single_cell(void)
{
cout<<Hi from C ++;
$ b 所以当我使用C时,它工作正常,但使用C ++函数时,它会给出错误,例如
对cout等的未定义错误。
解决方案
g ++和gfortran都用作链接器,带来额外的图书馆。这就是为什么Fortran / C ++组合比Fortran / C组合更复杂的原因......只需使用正确的编译器作为链接器就行不通,您需要添加一个库。已经建议与gfortran链接并指定C ++运行时库。您还可以链接到g ++并指定Fortran运行时库。有关这两种方法的详细信息,请参阅使用gcc链接fortran和c ++二进制文件 。
is it possible to call a C++ function from FORTRAN such as
#include <iostream.h>
extern "C"
{
void single_cell(void)
{
cout<<"Hi from C++";
}
}
So when I am using C it is working fine but with the C++ function it gives errors like
Undefined error to cout etc
解决方案 Both g++ and gfortran, used as linkers, bring in extra libraries. That is why the Fortran/C++ combination is trickier than the Fortran/C combination ... just using the correct compiler as the linker won't work, you need to add a libary. Already suggested is to link with gfortran and specify the C++ runtime libraries. You can also link with g++ and specify the Fortran runtime libraries. See Linking fortran and c++ binaries using gcc for the details of both approaches.
这篇关于从fortran调用C ++函数而不是C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!