使用Fortran调用C++功能

发布于 2024-08-25 17:38:14 字数 375 浏览 2 评论 0原文

我正在尝试获取一些 FORTRAN 代码来调用我编写的几个 C++ 函数(c_tabs_ 是其中之一)。只要我调用不属于某个类的函数,链接和一切都会正常工作。

我的问题是我希望 FORTRAN 代码调用的函数属于一个类。我使用 nm 查看了符号表,函数名称是这样的难看:

00000000 T _ZN9Interface7c_tabs_Ev

FORTRAN 不允许我用该名称调用函数,因为开头有下划线,所以我不知所措。

当 c_tabs 不在类中时,它的符号非常简单,FORTRAN 对此没有任何问题:

00000030 T c_tabs_

有什么建议吗?提前致谢。

I'm trying to get some FORTRAN code to call a couple c++ functions that I wrote (c_tabs_ being one of them). Linking and everything works just fine, as long as I'm calling functions that don't belong to a class.

My problem is that the functions I want the FORTRAN code to call belong to a class. I looked at the symbol table using nm and the function name is something ugly like this:

00000000 T _ZN9Interface7c_tabs_Ev

FORTRAN won't allow me to call a function by that name, because of the underscore at the beginning, so I'm at a loss.

The symbol for c_tabs when it's not in a class is quite simple, and FORTRAN has no problems with it:

00000030 T c_tabs_

Any suggestions? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青衫负雪 2024-09-01 17:38:15

您必须创建 extern "C" 包装器来处理 FORTRAN 调用 C++ 的所有细节,名称修改是最明显的。

class foo {
public:
    int a_method (int x);
}

extern "C" int foo_a (foo * pfoo, int * px) {
    if (NULL == pfoo)
        return 0;
    else
        return pfoo->a_method (*px);
}

请注意,FORTRAN 编译器通过引用传递所有参数,而不是通过值传递。 (尽管我被告知严格来说这不是 FORTRAN 标准的一部分。)

You have to create extern "C" wrappers to handle all the details of FORTRAN calling C++, name mangling being the most obvious.

class foo {
public:
    int a_method (int x);
}

extern "C" int foo_a (foo * pfoo, int * px) {
    if (NULL == pfoo)
        return 0;
    else
        return pfoo->a_method (*px);
}

Notice that FORTRAN compilers pass all arguments by reference, never by value. (Although I'm told this is not strictly speaking part of the FORTRAN standard.)

抱着落日 2024-09-01 17:38:14

该名称已被破坏,这是 C++ 编译器对函数所做的操作,以允许函数重载和类型安全链接等操作。坦率地说,您极不可能从 FORTRAN 中调用成员函数(因为 FORTRAN 无法创建 C++ 类实例等原因) - 您应该用 C API 来表达您的接口,它可以从几乎任何地方调用。

The name has been mangled, which is what the C++ compiler does to functions to allow things like function overloading and type-safe linkage. Frankly, you are extremely unlikely to be able to call member functions from FORTRAN (because FORTRAN cannot create C++ class instances, among other reasons) - you should express your interface in terms of a C API, which will be callable from just about anywhere.

贱人配狗天长地久 2024-09-01 17:38:14

您将需要创建一个 C 风格的接口并“extern”它。 C++ 修饰方法名称(和重载函数)以进行链接。众所周知,将 C++ 与除 C++ 之外的任何内容联系起来都是非常困难的。有一些“方法”,但我强烈建议您简单地导出 C 接口并使用 Fortran 中提供的标准设施。

You will need to create a c-style interface and "extern" it. C++ mangles method names (and overloaded functions) for linking. It's notoriously difficult to link C++ with anything except C++. There are "ways" but I'd highly suggest that you simply export a C interface and use the standard facilities available in Fortran.

攒眉千度 2024-09-01 17:38:14

如果您使 C++ 例程具有 C 风格的接口(如前所述),则可以使用 Fortran 2003 的 ISO C 绑定功能来调用它。通过 ISO C 绑定,您可以指定例程的名称以及(在限制范围内)参数和函数返回的 C 类型和调用约定(引用、按值)。与从 Fortran 调用 C 的旧方法不同,此方法运行良好,并且具有作为标准的优点,因此依赖于编译器和平台。许多 Fortran 95 编译器都支持 ISO C 绑定,例如 gfortran >= 4.3。

If you make the C++ routine have a C-style interface (as described already), then you can use the ISO C Binding feature of Fortran 2003 to call it. With the ISO C Binding, you can specify the name of the routine and (within limits) the C-types and calling conventions (reference, by value) of the arguments and function return. This method works well and has the advantage of being a standard, and therefore compiler and platform dependent, unlike the old methods of calling C from Fortran. The ISO C Binding is supported by many Fortran 95 compilers, such as gfortran >= 4.3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文