开发者

Fortran 77 conversion to C++

I have a Fortran 77 application that uses Common declarations to "share" memory variables. 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.

Any suggestions on how to implement the Common functionality in c++?

A class perhaps, with all the common variabl开发者_JAVA技巧es being public. Thus any program instantiating that class will have access to the contents of the Common variables.

Are there any tutorials/guidelines for converting Fortran to C or C++?

Thanks


This Fortran to C/C++ Tutorial suggests:

      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.

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.


First of all, you can get rid of common blocks by using Fortran 90 modules.

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.

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. 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.


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. The result will not be good c++ code, but it would give a place to start refactoring.

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.


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.


Don't do it at all, common blocks are an anathema in the modern day.


Strange as it may seem, this may be a place were c like bitfield should be used.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜