将 FORTRAN 转换为 C / C++

发布于 2024-10-05 08:25:01 字数 1145 浏览 7 评论 0原文

作为我的期末项目的一部分,我需要将一些 FORTRAN 代码转换为 C 或 C++(哪种语言并不重要,只要我能理解它,并且我能理解 C 风格语言)。

我发现了 f2c,一个据称将 FORTRAN 转换为 C 的程序,并尝试按照说明安装它,方法是在我的驱动器上保存 makefile.vc 文件,然后执行

copy makefile.vc makefile
nmake

(这是自述文件中有关安装 f2c 的部分,即包含在 f2c 下载页面中)

在 Linux 或 Unix 上编译 f2c 系统,将 makefile.u 复制到 makefile, 如有必要,编辑 makefile(请参阅 其中和下面的评论)并输入 “make”(或者可能是“nmake”,具体取决于 您的系统)。

在 MS Windows 上编译 f2c.exe 采用 Microsoft Visual C++ 的系统,

复制makefile.vc makefile nmake

对于其他 PC 编译器,您可能需要 使用 -DMSDOS 编译 xsum.c (即 与 MSDOS #定义)。

如果你的编译器不理解 ANSI/ISO C 语法(即,如果您有 K&RC 编译器),编译时使用 -DKR_headers 。

在非 Unix 系统上,文件具有 单独的二进制和文本模式,您 可能需要“make xsumr.out”而不是 比“make xsum.out”。

我正在运行 x64 位版本的 Windows Vista 并尝试了“nmake”,但我得到

“nmake”不被识别为内部或外部命令、可操作程序或批处理文件。

经过一番搜索后,我下载了 Nmake15.exe,但它无法在 x64 位计算机上运行,​​并且显然没有可以运行的版本。因此,在被告知可以使用后,我下载了 Windows SDK,但它没有改变任何东西。

如果我有的话,我在这一切中哪里出了错?有什么方法可以将 FORTRAN 代码转换为 C 或 C++ 吗?

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

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

copy makefile.vc makefile
nmake

(here is the part of the README file about installing f2c that is included in the f2c download page)

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

I am running x64 bit version of Windows Vista and tried "nmake", but I get

'nmake' is not recognised as an internal or external command, operable program or batch file.

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.

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++?

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

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

发布评论

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

评论(11

懒猫 2024-10-12 08:25:02

如果您有 F77 代码,f2c 确实是可行的方法。如果您有 F90 或更高版本,则 f2c 将无济于事。我已经多次使用 f2c 并取得了巨大成功,只要您记住 -a 开关即可。

顺便说一句,我将 f2c 评为有史以来最伟大的代码之一!

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.

As an aside I would rate f2c as one of the all time great codes!

榆西 2024-10-12 08:25:02

你需要清楚为什么要这样做。是因为您需要的功能仅在某些旧版 FORTRAN 中可用吗?很多年前,当我需要一个通用的矩阵求逆算法时,我就遇到了这个问题,而该算法仅在 FORTRAN 中可用。这并不容易理解 - 没有像 G(J) 这样命名的注释和变量。我使用 f2c 将其转换为 C,并且运行完美。但这更难理解。两点是FORTRAN从1开始计数,C从0开始计数,所以有很多i+1和j-1。参数也必须通过引用来实现。

后来我不得不用Java来运行它。仍然没有其他算法,所以我将C转换为Java。这真的很痛苦。我仍然不明白发生了什么事。

一两年后它就停止工作了!

但幸运的是,现在有几种 Java 实现。

因此,如果您能解释您的真实需求,也许我们可以提供帮助。我希望这不是一项任务,因为如果是这样的话(在我看来)这就是一项糟糕的任务。如果有一些神奇的遗留代码,建议您尽可能努力找到现代的等效代码。

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.

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.

And after a year or two it stopped working!

But, luckily, now there are several Java implementations.

So if you can explain your real requirements maybe we can help. I hope that 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.

携君以终年 2024-10-12 08:25:02

您对此并不太清楚,但听起来您想研究代码,并且为了做到这一点,您希望将其转换为 C 或 C++。如果您想使用代码而不是研究它,那么我在这里就大错特错了,您应该寻找 Fortran 编译器。

放弃。您找不到任何将惯用的 Fortran 语言转换为惯用的 C 或 C++ 的方法。语言之间存在微妙的语义差异,为了使其正确,翻译程序需要非常精确,并考虑很多事情,并进行模糊的函数调用,这可能会非常令人困惑。你不会得到可读的 C 或 C++。

相反,您应该学习适当版本的 Fortran。如果您了解 C 和 C++,那么学习它应该一点也不难。它只是另一种语言,不会有太多新概念。

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.

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

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.

你另情深 2024-10-12 08:25:02

到目前为止,我一直使用的转换方法是半自动化的:大量的编辑器和文本脚本。

首先,将所有 Fortran 源文件合并到一个文件中(适当放置标记以允许重新分隔文件)。

接下来,将数字标签转换为命名标签(例如,在其前面添加 L 表示 goto,添加 F 表示格式)。在引用标签的地方进行适当的更改。

接下来,标记标签-继续组合和语句延续(例如,在它们出现的每行开头标记一个标记)。然后重新连接 label-continue 和 statements-continuation 行。在像 vi 这样的编辑器中,这意味着执行一系列(例如)1000J,然后如果行标记的开头是 #,则进行大量替换:%s/#/^V^M/g。由于连续线与 do-continue 组合一样被特别标记,因此它们会重新连接。

确保 if 标签和 do 标签是分开的(例如,编写一个 grep 模式来提取相关行,然后编写一个简单的括号匹配程序来检查它们)。分开后,您可以将 do-continue 变成括号。

将 if-then-else、dowhile/enddo、subroutine/endsub 等转入括号内。使子例程无效,为基本类型(例如布尔、字符、整数、实数、双精度、复数、复数)使用假名称。

注释被转换为 C++ // 注释;第 0 列和 !评论。

然后您可以重新设计函数原型。这实际上是 KR 到 ANSI-C 的转换,需要一个简单的程序来读取函数头和变量声明以将它们匹配。理想情况下,您可能希望首先将变量声明作为一个放在一行中。这还需要一个编辑脚本和一个实用程序。例如,“Double A、B、C、...”将转换为“#Double\nA\nB\n”。然后设置一个实用程序,为所有非 # 行添加最近的 # 行前缀。

参数语句重新连接到类型声明并转换为“const”语句。保存被转换为静态声明。 Commons 我还没有找到一个好的工作方式。

现在接下来是困难部分:数组尺寸、变量输入/输出分析(以确定哪些变量需要按值或按引用传递)。您应该获得一个能够识别引用类型和引用类型的 C 编译器。或者使用C++。对于数组重新定义尺寸,您可能还需要使用允许重新定义 [] 的 C++。

进出分析是我还没有完成的事情,同样的尺寸调整也是如此。但在
就这一点而言,现阶段相关源码大多是C或C++。

请注意:Fortran 2010 允许并行处理,这超越了 C 甚至 C++(除了它们的多线程能力)。如果你的程序中有这些,事情就会变得更加困难。

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

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.

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.

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.

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

Comments are converted over to C++ // comments; both the column 0 and ! comments.

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.

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.

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.

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.

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.

小猫一只 2024-10-12 08:25:02

如果我必须在 Windows 上安装 f2c,我会使用 Cygwin。现在有了g77,估计不会有多少人再用f2c了。

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.

秉烛思 2024-10-12 08:25:02

FORTRAN 到 C 或 C++ 过程版本之间的直接转换应该不会很复杂,建议这样做主要是因为您将了解 FORTRAN 实现的内部工作原理。

f2c 这样的自动工具会生成带有双下划线的混乱代码,如下所示:__i(i 是循环计数器变量)。

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.

Automatic tools like f2c will produce messy code with double underscore like this: __i (i being the loop counter variable).

习惯成性 2024-10-12 08:25:02

我只回答你问题的最后一部分:

有什么办法可以转换吗
FORTRAN 代码转换为 C 或 C++?

是的,有:阅读 Fortran 代码,并在 C 文件中编写等效的 C 语句。
它们都是命令式语言。语法不同,但并非无法解码。这可能会给您带来比自动转换器更好的结果,并且还会让您更深入地了解原始程序的工作原理。

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++?

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.

错々过的事 2024-10-12 08:25:02

您是否尝试过二进制文件

Have you tried the binary?

短暂陪伴 2024-10-12 08:25:02

你安装了MS的Visual Studio吗?如果是这样,则 nmake 可在 Visual Studio 命令行(而不是普通命令行)上使用。如果没有,也许尝试下载并安装它?

Do you have MS's Visual Studio installed? If so, nmake is available on the Visual Studio Command Line (not the normal command line). If not, perhaps try downloading and installing that?

‖放下 2024-10-12 08:25:02

f2c 的输出可以转换为更好可读的 C/C++ 源代码使用此处提供的f2cpp程序。

它是用 Python 编写的,可以在 Linux 下通过 shell 脚本与 f2c 一起调用

#!/bin/bash

f2c -a -C++ -p *.f
c2cpp.py *.c

,其中 f2c 将 Fortran 固定格式转换为 C 和 c2cpp在人类可读的 C/C++ 源代码中。

许可证是 GPL。

The output of f2c could be converted into better readable C/C++ sources by using the f2cpp program which is available here.

It's written in Python and could be called together with f2c under Linux by e.g. a shell script

#!/bin/bash

f2c -a -C++ -p *.f
c2cpp.py *.c

where f2c converts the Fortran fixed format into C and c2cpp in human readable C/C++ sources.

License is GPL.

靑春怀旧 2024-10-12 08:25:01

我发现有一个名为fable的小工具包 fable - 自动 Fortran 到 C++ 转换 专门用于此类转换。

还有对此类工具的评论。综述作者摘要:

背景

在科学计算领域,Fortran 在 20 世纪下半叶的大部分时间里都是占主导地位的实现语言。这段时间积累的很多工具已经很难与现在以面向对象语言为主的现代软件集成。

结果

受大型科学软件项目需求的驱动,我们开发了一款名为 FABLE 的 Fortran 到 C++ 源代码转换工具。即使在切换语言的情况下,这也使得新方法的持续开发成为可能。我们报告了 FABLE 在三个主要项目中的应用,并详细比较了 Fortran 和 C++ 运行时性能。

结论

我们的经验表明,与原始开发时间(通常以年为单位)相比,大多数 Fortran 77 代码的转换工作量很小(以天为单位)。借助 FABLE,可以在现代面向对象的环境中以可移植且可维护的方式重用和发展遗留工作。 FABLE 可在非限制性开源许可证下使用。在 FABLE 中,Fortran 源代码的分析与 C++ 源代码的生成是分开的。因此,FABLE 的部分内容可以重用于其他目标语言。

I found there is a small toolkit named fable fable - Automatic Fortran to C++ conversion which is dedicated to such 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.

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