致电 C/C++来自 Fortran 77 代码的代码

发布于 2024-09-09 02:46:52 字数 110 浏览 2 评论 0原文

我正在尝试为 C++ 代码制作 Fortran 77 包装器。我还没有找到相关信息。 这个想法是使用 Fortran 77 程序中用 C++ 编写的库中的函数。

有谁知道该怎么做? 谢谢!

I'm trying to make a Fortran 77 wrapper for C++ code. I have not found information about it.
The idea is to use from functions from a lib that is written in C++ in a Fortran 77 progran.

Does anyone know how to do it?
Thanks!

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

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

发布评论

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

评论(2

勿忘心安 2024-09-16 02:46:52

劳伦斯利弗莫尔国家实验室开发了一种名为 Babel 的工具,用于集成软件用多种语言编写成一个单一的、有凝聚力的应用程序。如果您的需求很简单,您可能只需将 C 包装器放在 C++ 代码上并从 Fortran 中调用它即可。但是,如果您的需求更高级,则可能值得提供 Babel< /a> 一看。

Lawrence Livermore National Laboratory developed a tool called Babel for integrating software written in multiple languages into a single, cohesive application. If your needs are simple you can probably just put C wrapper on your C++ code and call that from Fortran. However, if your needs are more advanced, it might be worth giving Babel a look.

初见你 2024-09-16 02:46:52

从 C 调用 Fortran 很容易,从 Fortran 调用 C 可能很棘手,从 Fortran 调用 C++ 可能会变得……具有挑战性。

我在其他地方有一些注释< /a>.这些已经很古老了,但是在这种领域没有什么变化很快,所以那里可能仍然有一些有用的指针。

不幸的是,没有真正标准的方法来执行此操作,并且不同的编译器可能会以稍微不同的方式执行此操作。话虽如此,只有在传递字符串时您才可能遇到严重的麻烦。上面的资源指向一个名为 CNF 旨在在这里提供帮助,主要是通过提供 C 宏来糖化簿记。

然而,简短的版本是这样的:

  • 浮点数和整数通常很容易——整数或多或少都是整数。
  • 字符串很难(因为 Fortrans 经常将它们存储为结构,很少存储为 C 风格的 null 终止数组)。
  • C 是按值调用,Fortran 是按引用调用,这意味着从 C 的角度来看,Fortran 函数始终是指向值的指针。
  • 您必须关心编译器如何生成符号:编译器经常将 C/Fortran 符号 foo 转换为 _foofoo_ 或其他一些变体(请参阅编译器文档)。
  • C 往往没有太多的运行时,而 C++ 和 Fortran 有,因此您必须记住在链接时以某种方式链接它。

这是您需要了解的大部分内容。剩下的就是烦人的细节,以及与编译器和链接器文档交朋友。您最终会比您想要的更多地了解链接器。

Calling Fortran from C is easy, C from Fortran potentially tricky, C++ from Fortran may potentially become ... challenging.

I have some notes elsewhere. Those are quite old, but nothing changes very rapidly in this sort of area, so there may still be some useful pointers there.

Unfortunately, there's no really standard way of doing this, and different compilers may do it slightly different ways. Having said that, it's only when passing strings that you're likely to run into major headaches. The resource above points to a library called CNF which aims to help here, mostly by providing C macros to sugar the bookkeeping.

The short version, however is this:

  • Floats and integers are generally easy -- an integer is an integer, more or less.
  • Strings are hard (because Fortrans quite often store these as structures, and very rarely as C-style null-terminated arrays).
  • C is call-by-value, Fortran call-by-reference, which means that Fortran functions are always pointer-to-value, from C's point of view.
  • You have to care about how your compiler generates symbols: compilers often turn C/Fortran symbol foo into _foo or foo_ or some other variant (see the compiler docs).
  • C tends not to have much of a runtime, C++ and Fortran do, and so you have to remember to link that in somehow, at link time.

That's the majority of what you need to know. The rest is annoying detail, and making friends with your compiler and linker docs. You'll end up knowing more about linkers than you probably wanted to.

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