简体   繁体   English

Fortran 77转换为C ++

[英]Fortran 77 conversion to C++

I have a Fortran 77 application that uses Common declarations to "share" memory variables. 我有一个Fortran 77应用程序,它使用Common声明来“共享”内存变量。 Back in the day, when memory was expensive and hard to come by this was the way around it. 回到白天,当记忆力昂贵且很难得到时,这就是它的方式。

The equipment being controlled sets status flags. 被控制的设备设置状态标志。 These flags reside in these Common variables. 这些标志位于这些Common变量中。

Any suggestions on how to implement the Common functionality in c++? 有关如何在c ++中实现通用功能的任何建议?

A class perhaps, with all the common variables being public. 也许是一个类,所有公共变量都是公共的。 Thus any program instantiating that class will have access to the contents of the Common variables. 因此,任何实例化该类的程序都可以访问Common变量的内容。

Are there any tutorials/guidelines for converting Fortran to C or C++? 有没有将Fortran转换为C或C ++的教程/指南?

Thanks 谢谢

First of all, you can get rid of common blocks by using Fortran 90 modules. 首先,您可以使用Fortran 90模块去掉常见的块。

If you do want to directly convert common blocks to C++ you would have to make a whole bunch of global/static variables or use an unnamed namespace. 如果您确实想要将公共块直接转换为C ++,则必须创建一大堆全局/静态变量或使用未命名的命名空间。

However, that violates information hiding and most people would advise you against thoughtless use of global variables. 然而,这违反了信息隐藏,大多数人会建议你不要轻率地使用全局变量。

More generally, you might be interested in looking up the Barton-Nackman book Scientific and Engineering C++: An Introduction with Advanced Techniques and Examples . 更一般地说,您可能有兴趣查阅Barton-Nackman的书“ 科学与工程C ++:高级技术与实例介绍” It's a bit outdated, but that shouldn't matter too much. 它有点过时,但这不应该太重要。 It teaches you C++ for scientific or engineering applications assuming that you have a Fortran / procedural background. 它教您使用C ++进行科学或工程应用程序,假设您具有Fortran /程序背景。

This Fortran to C/C++ Tutorial suggests: 这个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_;
         }

You put the extern struct into an .h file which the C / C++ files refer to with #include, and in exactly one .c or .cpp file you put exactly what was in the .h file but without the "extern" word. 你将extern结构放入一个.h文件中,C / C ++文件用#include引用它,并且在一个.c或.cpp文件中你准确地放入了.h文件但没有“extern”字。

My assumption is that what you have to start with is relatively terse and borderline incomprehensible, and that you want to translate it into C++ in a way that preserves a near one-to-one correspondence to the original. 我的假设是,你必须从相对简洁和边界不可理解的开始,并且你想要以一种与原始文件保持近似一一对应的方式将其翻译成C ++。

Apropos to my comment on Alexandros' answer the "natural" transliteration is to include everything in the common blocks as class statics in the c++ code. 我对Alexandros回答“自然”音译的评论是在c ++代码中包含common块中的所有内容作为类静态。 The result will not be good c++ code, but it would give a place to start refactoring. 结果将不是好的c ++代码,但它会给一个开始重构的地方。

That said, I would usually try to interface a c++ front end to the existing fortran back end first, and then start the translation process if it still seems like a good idea. 也就是说,我通常会先尝试将c ++前端与现有的fortran后端连接起来, 然后再开始翻译过程,如果它仍然是一个好主意。

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. 将这些转换为C extern should工作,但是你真的需要掌握设备接口的文档,以确保你正确地执行它。

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. 如果您有时间和信心,最好的方法是拥有一个静态类,它处理公共存储的所有更新,并用“get”和“set”替换对新类的所有读写操作。

不要这样做,普通的块在现代是一种诅咒。

Strange as it may seem, this may be a place were c like bitfield should be used. 看起来很奇怪,这可能是一个像应该使用bitfield的地方。

He has a piece of hardware, which probably has various control registers and status flags, mapped to some fixed memory address. 他有一块硬件,可能有各种控制寄存器和状态标志,映射到一些固定的内存地址。

A struct with correctly laid out bit fields (compiler specific), with a pointer of that type pointing to the correct address may do the trick. 具有正确布局的位字段(特定于编译器)的结构,以及指向正确地址的该类型的指针可以起到作用。

As the field values can change without notice, the violatile qualifer may be needed. 由于场地值可能会在没有通知的情况下发生变化,因此可能需要使用违规物品。

It would help if the questioner would provide more information on the layout of the common block, and the interpretation of the data within it. 如果提问者提供有关公共区块布局的更多信息,以及对其中数据的解释,将会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM