调用 C++来自 Fortran 而非 C 的函数
是否可以从 FORTRAN 调用 C++ 函数,例如
#include <iostream.h>
extern "C"
{
void single_cell(void)
{
cout<<"Hi from C++";
}
}
因此,当我使用 C 时,它工作正常,但使用 C++ 函数时,它会给出如下错误 cout 等的未定义错误
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用作链接器的 g++ 和 gfortran 都引入了额外的库。这就是为什么 Fortran/C++ 组合比 Fortran/C 组合更棘手......仅使用正确的编译器作为链接器是行不通的,您需要添加一个库。已经建议与 gfortran 链接并指定 C++ 运行时库。您还可以链接 g++ 并指定 Fortran 运行时库。有关这两种方法的详细信息,请参阅使用 gcc 链接 fortran 和 c++ 二进制文件 。
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++ 对象时,您还需要引入 C++ 运行时。如果使用 GCC,请与 g++ 命令链接,它将提取您需要的部分。
Assuming you could have your Fortran code call into a C function, the problem is not the code but rather how you are linking. When you're linking C++ objects you need to also pull in the C++ runtime. If using GCC, link with the g++ command and it will pull in the parts you need.