3

我有一个 Fortran 77 应用程序,它使用通用声明来“共享”内存变量。在过去,当内存昂贵且难以获得时,这就是它的解决方法。

被控制的设备设置状态标志。这些标志位于这些公共变量中。

关于如何在 C++ 中实现通用功能的任何建议?

也许是一个类,所有公共变量都是公共的。因此,任何实例化该类的程序都可以访问公共变量的内容。

是否有任何将 Fortran 转换为 C 或 C++ 的教程/指南?

谢谢

4

6 回答 6

2

这个 Fortran 到 C/C++ 教程建议:

      FORTRAN:
           DOUBLE PRECISION X
           INTEGER A, B, C
           COMMON/ABC/ X, A, B, C

      C:
           extern struct{
               double x;
               int a, b, c;
           } abc_;

      C++:
         extern "C" {
           extern struct{
               double x;
               int a, b, c;
           } abc_;
         }

您将 extern 结构放入 C / C++ 文件使用#include 引用的 .h 文件中,并且在恰好一个 .c 或 .cpp 文件中,您准确地放入了 .h 文件中的内容,但没有“extern”字样。

我的假设是,您必须开始的内容相对简洁且难以理解,并且您希望以一种与原始文件几乎一一对应的方式将其翻译成 C++。

于 2010-11-30T00:34:47.700 回答
2

首先,您可以使用 Fortran 90 模块摆脱常见的块。

如果您确实想直接将公共块转换为 C++,则必须创建一大堆全局/静态变量或使用未命名的命名空间。

但是,这违反了信息隐藏,大多数人会建议您不要轻率地使用全局变量。

更一般地说,您可能有兴趣查找 Barton-Nackman 的书Scientific and Engineering C++: An Introduction with Advanced Techniques and Examples。它有点过时了,但这不应该太重要。假设您具有 Fortran / 程序背景,它会教您用于科学或工程应用程序的 C++。

于 2010-11-30T00:17:31.503 回答
1

I know I am reapeating what I said in a comment, but, I dont think anyone got it.

The phrase "equipment being controlled" signals to me that the program is some sort of device driver and its highly likely that the device is expecting a its flags to be in a specific area of memory. The reason common storage is being used is that the various modules can access and update these areas directly. Translating these into a C extern should work but you would really need to get hold of the documentation for the device interface to make sure you are doing it properly.

Losing the common storgage as some posters suggest will simply not work under these circumstances. The best approach if you have the time and confidence would be to have a static class which handles all updates to the common storage and to replace all reads and writes to common storage with "get" s and "set"s to the new class.

于 2010-12-07T01:45:05.970 回答
1

对于我对 Alexandros 的回答的评论,“自然”的音译是将common块中的所有内容作为类静态包含在 c++ 代码中。结果不会是好的 c++ 代码,但它会提供一个开始重构的地方。

也就是说,我通常会先尝试将 c++ 前端连接到现有的 fortran 后端,然后如果它看起来仍然是个好主意,则开始翻译过程。

于 2010-11-30T00:32:38.800 回答
0

根本不要这样做,常见的块在现代是一种诅咒。

于 2010-11-30T19:33:48.047 回答
0

看起来很奇怪,这可能是一个应该使用 c 之类的位域的地方。

他有一个硬件,可能有各种控制寄存器和状态标志,映射到某个固定的内存地址。

具有正确布局的位字段(特定于编译器)的结构,该类型的指针指向正确的地址可能会起作用。

由于字段值可能会在没有通知的情况下更改,因此可能需要 violatile 限定符。

如果提问者能提供更多关于公共块布局的信息,以及对其中数据的解释,将会有所帮助。

于 2010-12-07T03:13:20.527 回答