将 FORTRAN 转换为 C/C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4314243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting FORTRAN to C / C++
提问by OddCore
As part of my Final Year Project, I need to convert some FORTRAN code into C or C++ (it doesn't matter which language as long as I can understand it, and I can understand C style languages).
作为我的最后一年项目的一部分,我需要将一些 FORTRAN 代码转换为 C 或 C++(只要我能理解它,并且我能理解 C 风格的语言,这并不重要)。
I have discovered f2c, a program that allegedly converts FORTRAN to C, and tried to install it, following instructions, by saving a makefile.vc file on my drive and then doing
我发现了 f2c,一个据称将 FORTRAN 转换为 C 的程序,并尝试按照说明安装它,方法是在我的驱动器上保存一个 makefile.vc 文件,然后执行
copy makefile.vc makefile
nmake
(here is the part of the README file about installing f2c that is included in the f2c download page)
(这里是 f2c下载页面中关于安装 f2c 的 README 文件的一部分)
To compile f2c on Linux or Unix systems, copy makefile.u to makefile, edit makefile if necessary (see the comments in it and below) and type "make" (or maybe "nmake", depending on your system).
To compile f2c.exe on MS Windows systems with Microsoft Visual C++,
copy makefile.vc makefile nmake
With other PC compilers, you may need to compile xsum.c with -DMSDOS (i.e., with MSDOS #defined).
If your compiler does not understand ANSI/ISO C syntax (i.e., if you have a K&R C compiler), compile with -DKR_headers .
On non-Unix systems where files have separate binary and text modes, you may need to "make xsumr.out" rather than "make xsum.out".
要在 Linux 或 Unix 系统上编译 f2c,请将 makefile.u 复制到 makefile,必要时编辑 makefile(请参阅其中和下面的注释)并键入“make”(或者可能是“nmake”,具体取决于您的系统)。
要在 MS Windows 系统上使用 Microsoft Visual C++ 编译 f2c.exe,
复制makefile.vc makefile nmake
对于其他 PC 编译器,您可能需要使用 -DMSDOS(即使用 MSDOS #defined)编译 xsum.c。
如果您的编译器不理解 ANSI/ISO C 语法(即,如果您有 K&R C 编译器),请使用 -DKR_headers 进行编译。
在文件具有单独的二进制和文本模式的非 Unix 系统上,您可能需要“make xsumr.out”而不是“make xsum.out”。
I am running x64 bit version of Windows Vista and tried "nmake", but I get
我正在运行 x64 位版本的 Windows Vista 并尝试了“nmake”,但我得到了
'nmake' is not recognised as an internal or external command, operable program or batch file.
'nmake' 不是内部或外部命令,也不是可运行的程序或批处理文件。
I downloaded Nmake15.exe after some searching but it doesn't work on x64 bit machines and apparently there is not version of it that does. So I downloaded the Windows SDK, after being told that would work, but it didn't change anything.
经过一番搜索,我下载了 Nmake15.exe,但它在 x64 位机器上不起作用,而且显然没有它的版本。所以我下载了 Windows SDK,在被告知可以工作后,但它没有改变任何东西。
Where have I gone wrong in all this, if I have, and is there any way of converting that FORTRAN code into C or C++?
我在这一切中哪里出了问题,如果有的话,有没有办法将 FORTRAN 代码转换为 C 或 C++?
采纳答案by AShelly
I'm going to answer only the very last part of your question:
我只回答你问题的最后一部分:
is there any way of converting that FORTRAN code into C or C++?
有没有办法将该 FORTRAN 代码转换为 C 或 C++?
Yes there is: Read the fortran code, and write the equivalent C statements in a C file. They are both imperative languages. The syntax is different but not impossible to decode. This will likely give you much better results than an auto-converter, and will also give you an much deeper understanding of how the original program works.
是的,有:阅读 fortran 代码,并在 C 文件中编写等效的 C 语句。它们都是命令式语言。语法不同,但并非不可能解码。与自动转换器相比,这可能会给您带来更好的结果,并且还将让您更深入地了解原始程序的工作原理。
回答by LCFactorization
I found there is a small toolkit named fablefable - Automatic Fortran to C++ conversionwhich is dedicated to such conversion.
我发现有一个名为fable fable - Automatic Fortran to C++ conversion的小工具包,专门用于这种转换。
THere is also a review of such a tool. Abstract from the review authors:
还有对此类工具的评论。综述作者摘要:
Background
In scientific computing, Fortran was the dominant implementation language throughout most of the second part of the 20th century. The many tools accumulated during this time have been difficult to integrate with modern software, which is now dominated by object-oriented languages.
Results
Driven by the requirements of a large-scale scientific software project, we have developed a Fortran to C++ source-to-source conversion tool named FABLE. This enables the continued development of new methods even while switching languages. We report the application of FABLE in three major projects and present detailed comparisons of Fortran and C++ runtime performances.
Conclusions
Our experience suggests that most Fortran 77 codes can be converted with an effort that is minor (measured in days) compared to the original development time (often measured in years). With FABLE it is possible to reuse and evolve legacy work in modern object-oriented environments, in a portable and maintainable way. FABLE is available under a nonrestrictive open source license. In FABLE the analysis of the Fortran sources is separated from the generation of the C++ sources. Therefore parts of FABLE could be reused for other target languages.
背景
在科学计算领域,Fortran 是 20 世纪下半叶大部分时间的主要实现语言。这段时间积累的许多工具很难与现代软件集成,现代软件现在以面向对象的语言为主。
结果
在大型科学软件项目需求的推动下,我们开发了一款名为 FABLE 的 Fortran 到 C++ 源到源转换工具。这使得即使在切换语言时也能继续开发新方法。我们报告了 FABLE 在三个主要项目中的应用,并提供了 Fortran 和 C++ 运行时性能的详细比较。
结论
我们的经验表明,与原始开发时间(通常以年为单位)相比,大多数 Fortran 77 代码的转换工作量很小(以天为单位)。使用 FABLE,可以在现代面向对象的环境中以可移植和可维护的方式重用和发展遗留工作。FABLE 在非限制性开源许可下可用。在 FABLE 中,Fortran 源代码的分析与 C++ 源代码的生成是分开的。因此,部分 FABLE 可以重用于其他目标语言。
回答by David Heffernan
f2c really is the way to go, provided you have F77 code. If you have F90 or later then f2c won't help. I've used f2c many many times with great success, so long as you remember the -a switch.
f2c 确实是要走的路,前提是您有 F77 代码。如果您有 F90 或更高版本,那么 f2c 将无济于事。我已经多次使用 f2c 并取得了巨大的成功,只要你记得 -a 开关。
As an aside I would rate f2c as one of the all time great codes!
顺便说一句,我将 f2c 评为有史以来最棒的代码之一!
回答by peter.murray.rust
You need to be clear why you are doing this. Is it because the functionality you need is ONLY available in some legacy FORTRAN? I had this problem many years ago when I needed a general matrix inversion algorithm, that was only available in FORTRAN. It wasn't easy to understand - no comments and variables named like G(J). I converted it to C using f2c and it ran perfectly. But it was even harder to understand. Two points were that FORTRAN counts from 1 and C from 0, so there were lots of i+1 and j-1. Also arguments had to be implemented by reference.
你需要清楚你为什么要这样做。是否因为您需要的功能仅在某些旧版 FORTRAN 中可用?多年前,当我需要通用矩阵求逆算法时,我遇到了这个问题,该算法仅在 FORTRAN 中可用。这并不容易理解 - 没有像 G(J) 这样命名的注释和变量。我使用 f2c 将它转换为 C 并且它运行得很好。但更难理解。两点是 FORTRAN 从 1 开始计数,C 从 0 开始计数,因此有很多 i+1 和 j-1。参数也必须通过引用来实现。
Later I had to run this in Java. Still no other algorithm, so I converted the C to Java. This was really painful. And I still didn't understand what has happening.
后来我不得不在Java中运行它。仍然没有其他算法,所以我将C转换为Java。这真的很痛苦。我仍然不明白发生了什么。
And after a year or two it stopped working!
一两年后它停止工作了!
But, luckily, now there are several Java implementations.
但是,幸运的是,现在有几个 Java 实现。
So if you can explain your real requirements maybe we can help. I hopethat this isn't an assignment, because if so it's (IMO) a poor one. If there is some magic legacy code, suggest you try as hard as possible to find a modern equivalent.
因此,如果您能解释您的真实需求,也许我们可以提供帮助。我希望这不是一项任务,因为如果是这样,它(IMO)就是一个糟糕的任务。如果有一些神奇的遗留代码,建议您尽可能努力寻找现代的等价物。
回答by Mark Hopkins
The method I've been using for conversion so far is semi-automated: lots of editor and text scripts.
到目前为止,我一直用于转换的方法是半自动化的:大量的编辑器和文本脚本。
First, collimate all the Fortran source into a single file (placing markers as appropriate to allow the files to be re-separated).
首先,将所有 Fortran 源代码对齐到一个文件中(适当放置标记以允许重新分隔文件)。
Next, convert the numeric labels to named labels (e.g. adding an L before them for gotos and F for formats). Make the appropriate changes in the places that reference the labels.
接下来,将数字标签转换为命名标签(例如,在它们之前添加 L 表示 goto 和 F 表示格式)。在引用标签的位置进行适当的更改。
Next, mark off the label - continue combinations and the statement continuations (e.g. with a mark at the start of each line where they occur). Then re-join the label-continue and statement-continuation lines. In an editor like vi, that would mean doing a series of (say) 1000J's, and then a massive substitute :%s/#/^V^M/g if the start of line marker is a #. Since the continuation lines were specially marked as were the do-continue combos, then they get rejoined.
接下来,标记标签 - 继续组合和语句继续(例如,在它们出现的每一行的开头标记)。然后重新加入 label-continue 和 statement-continuation 行。在像 vi 这样的编辑器中,这意味着要执行一系列(例如)1000J 的操作,然后大量替换:%s/#/^V^M/g,如果行标记的开头是 #。由于连续线被特别标记为 do-continue 组合,然后它们会重新加入。
Ensure the if-labels and do-labels are separated (e.g. write a grep pattern to extract the relevant lines and then write a simple bracket-matching program to check them out). Once separated, you can then turn the do-continues into brackets.
确保 if-labels 和 do-labels 是分开的(例如,编写一个 grep 模式来提取相关行,然后编写一个简单的括号匹配程序来检查它们)。分离后,您可以将 do-continue 变成括号。
Turn the if-then-else, and dowhile/enddo, subroutine/endsub, etc. into brackets. Make subroutine void, pony up fake names for the basic types (e.g. Boolean, Character, Integer, Real, Double, Complex, Complex).
把 if-then-else、dowhile/enddo、subroutine/endsub 等放到括号里。使子程序无效,为基本类型(例如布尔、字符、整数、实数、双精度、复数、复数)提供假名。
Comments are converted over to C++ // comments; both the column 0 and ! comments.
注释被转换为 C++ // 注释;第 0 列和 ! 注释。
Then you can re-prototype the functions. This is effectively a KR to ANSI-C conversion and requires a simple program to read the function headers and variable declarations to match them off. Ideally you may want to first put the variable declarations as one to a line. This also requires an edit script and a utility. For instance a "Double A, B, C, ..." would be converted to "#Double\nA\nB\n." and then a utility is set up to prefix all the non-# lines with the most recent preceding # line.
然后你可以重新设计函数。这实际上是 KR 到 ANSI-C 的转换,需要一个简单的程序来读取函数头和变量声明以匹配它们。理想情况下,您可能希望首先将变量声明放在一行中。这还需要一个编辑脚本和一个实用程序。例如,“Double A, B, C, ...”将被转换为“#Double\nA\nB\n”。然后设置一个实用程序,用最近的前 # 行作为所有非 # 行的前缀。
Parameter statements get rejoined to the type declarations and converted to "const" statements. Saves get converted to static declarations. Commons I haven't yet figured out a good way to work with.
参数语句重新加入类型声明并转换为“const”语句。保存被转换为静态声明。Commons 我还没有想出一个好的工作方式。
Now the HARD parts come next: the array dimensioning, variable in/out analysis (to determine which variables need to be passed by value or by reference). You should get a C compiler that recognizes the reference type & or use C++. For array-redimensioning you may also need to go over to C++ which allows [] to be redefined.
接下来是 HARD 部分:数组维度、变量输入/输出分析(以确定哪些变量需要按值或按引用传递)。您应该获得一个识别引用类型 & 或使用 C++ 的 C 编译器。对于数组重新维度,您可能还需要使用 C++,它允许重新定义 []。
The in-out analysis is something I haven't yet worked through, likewise the redimensioning. But at this point, the relevant source is mostly in C or C++ at this stage.
进出分析是我还没有完成的,同样是重新尺寸。但在这一点上,现阶段相关的源代码大多是 C 或 C++。
Be warned: Fortran 2010allows parallel processing, which goes beyond C and even C++ (except for their multithreaded abilities). If you programs have these in them, things will become a lot harder.
请注意:Fortran 2010 允许并行处理,它超越了 C 甚至 C++(除了它们的多线程能力)。如果你的程序中有这些,事情会变得更加困难。
回答by David Thornley
You aren't real clear on this, but it sounds like you want to study the code, and in order to do that you want to convert it to C or C++. If you want to use the code rather than study it, then I'm off-base here, and you should look for Fortran compilers.
您对此不太清楚,但听起来您想研究代码,为此您想将其转换为 C 或 C++。如果您想使用代码而不是研究它,那么我在这里不合时宜,您应该寻找 Fortran 编译器。
Give up. You can't find any way of translating idiomatic Fortran to idiomatic C or C++. There's subtle semantic differences between the languages, and in order to get it right the translation program needs to be very precise, and account for lots of things, and make obscure function calls, and that can be very confusing. You will not get readable C or C++.
放弃。您找不到任何将惯用的 Fortran 转换为惯用的 C 或 C++ 的方法。语言之间存在细微的语义差异,为了使其正确,翻译程序需要非常精确,并考虑很多事情,并进行晦涩的函数调用,这可能会非常混乱。你不会得到可读的 C 或 C++。
Instead, you should learn the appropriate version of Fortran. If you understand C and C++, it shouldn't be at all hard to learn. It's just another language, and it won't have much in the way of new concepts.
相反,您应该学习合适的 Fortran 版本。如果您了解 C 和 C++,那么学习它应该不难。它只是另一种语言,不会有太多新概念。
回答by David Thornley
If I had to install f2c on Windows I would use Cygwin. Now there is g77 I expect not many people use f2c any more.
如果我必须在 Windows 上安装 f2c,我会使用 Cygwin。现在有 g77 我希望没有多少人使用 f2c 了。
回答by beniekg
Direct conversion to/from FORTRAN to C or to procedural version of C++ should not be complicated and is recommended mainly because you will learn the inner working of FORTRAN implementation.
直接转换到/从 FORTRAN 到 C 或到 C++ 的过程版本不应该很复杂,建议这样做主要是因为您将学习 FORTRAN 实现的内部工作。
Automatic tools like f2c
will produce messy code with double underscore like this: __i
(i being the loop counter variable).
像这样的自动工具f2c
会产生带有双下划线的混乱代码,如下所示:(__i
我是循环计数器变量)。
回答by TZHX
Do you have MS's Visual Studio installed? If so, nmake is available on the Visual Studio Command Line (notthe normal command line). If not, perhaps try downloading and installing that?
你有安装 MS 的 Visual Studio 吗?如果是这样,nmake 在 Visual Studio 命令行(不是普通命令行)上可用。如果没有,也许尝试下载并安装它?