# GNU Fortran

**GNU Fortran**（GFortran）是 GCC（GNU 编译器集合）的 Fortran 编译器前端与运行时库，通过 `gfortran` 命令调用。它是 [GNU 工程](https://www.edgepedia.org/zh/gnu-project)开发的自由软件编译器，旨在将 Fortran 编译能力带给科学界、教育领域和商业环境，覆盖广泛的平台<sup>[1](https://gcc.gnu.org/fortran/)</sup>。

| 关键事实 | 详情 |
|---|---|
| 角色 | GCC 的 Fortran 编译器前端与运行时库；GNU 工程的一部分<sup>[1](https://gcc.gnu.org/fortran/)</sup> |
| 命令 | `gfortran`，最初设计为 Unix `f95` 命令的自由替代品<sup>[2](https://gcc.gnu.org/onlinedocs/gcc-4.8.4/gfortran/Introduction.html)</sup> |
| 标准 | 完整实现 Fortran 77、90 和 95；大部分 Fortran 2003 和 2008；部分 Fortran 2018 特性；初步支持 Fortran 2023<sup>[3](https://gcc.gnu.org/onlinedocs/gfortran/About-GNU-Fortran.html)</sup> |
| 并行 | OpenMP（`-fopenmp`）和 OpenACC（`-fopenacc`，面向 API v2.6），以及 Fortran 协数组<sup>[4](https://gcc.gnu.org/onlinedocs/gfortran/OpenACC.html)</sup><sup> • </sup><sup>[1](https://gcc.gnu.org/fortran/)</sup> |
| 进入 GCC 时间 | GCC 4.0.0 版本（2005 年）；前端于 2004 年 5 月合入 GCC 主干<sup>[1](https://gcc.gnu.org/fortran/)</sup><sup> • </sup><sup>[5](https://gcc.gnu.org/legacy-ml/fortran/2011-06/msg00142.html)</sup> |
| 验证 | 通过 NIST Fortran 77 测试套件；在 LAPACK 测试套件上结果可接受<sup>[3](https://gcc.gnu.org/onlinedocs/gfortran/About-GNU-Fortran.html)</sup> |
| 实际应用 | HARMONIE 和 HIRLAM 天气预报代码，以及 Tonto 量子化学软件包<sup>[3](https://gcc.gnu.org/onlinedocs/gfortran/About-GNU-Fortran.html)</sup> |

## 历史

GNU Fortran 项目于 2000 年 3 月 18 日在 GCC 主页上公布，不过 Andy Vaught（安迪·沃特）此前已经工作了一段时间；他在 2000 年 4 月的笔记中将项目的各个阶段描述为“幼虫”、“蛹”，以及完成时的“一只美丽的蝴蝶，或者只是一只大虫子”<sup>[6](https://gcc.gnu.org/onlinedocs/gcc-4.2.4/gfortran.pdf)</sup>。最初的目标是做一个符合 ISO/IEC 1539-1:1997 的 Fortran 95 编译器<sup>[6](https://gcc.gnu.org/onlinedocs/gcc-4.2.4/gfortran.pdf)</sup>。

该编译器是 `g77` 的后继者。`g77` 是 GCC 4 版之前附带的 [Fortran 77](https://www.edgepedia.org/zh/fortran-77) 前端，GCC 4 于 2005 年发布。`gfortran` 虽然仍兼容大多数 `g77` 扩展和命令行选项，但完全是从零写起，目标是处理更新的 Fortran 方言。<sup>[3](https://gcc.gnu.org/onlinedocs/gfortran/About-GNU-Fortran.html)</sup> 据 GCC 开发者 Janus Weil（雅努斯·魏尔）说，tree-ssa 分支上的首次前端提交由 Paul Brook（保罗·布鲁克）于 2003 年 7 月完成，提交日志为“gcc/fortran: 新前端”，前端于 2004 年 5 月合入 GCC 主干；他指出该提交很可能是 g95 的或多或少直接分叉，因此没有更早的提交历史留存<sup>[5](https://gcc.gnu.org/legacy-ml/fortran/2011-06/msg00142.html)</sup>。自 GCC 4.0.0 版本起，GNU Fortran 一直是该编译器集合的一部分<sup>[1](https://gcc.gnu.org/fortran/)</sup>。

## 标准支持

GNU Fortran 实现 ISO/IEC 1539:1997（Fortran 95），基本上能编译所有符合标准的 Fortran 90 和 Fortran 77 程序。它还支持 ISO/IEC TR-15581 对可分配数组的增强<sup>[7](https://gcc.gnu.org/onlinedocs/gfortran/Standards.html)</sup>。在此基线之上，编译器实现了几乎全部 Fortran 2003（ISO/IEC 1539-1:2004），但终结支持不完整；还实现了几乎全部 Fortran 2008（ISO/IEC 1539-1:2010）<sup>[8](https://gcc.gnu.org/onlinedocs/gfortran.pdf)</sup>。

对于 Fortran 2018（ISO/IEC 1539:2018），支持是部分的，但包括源自 ISO/IEC TS 29113:2012《Fortran 与 C 的进一步互操作性》的全部特性，如假定类型对象、假定秩对象以及 `SELECT RANK` 结构。ISO/IEC TS 18508:2015 的并行特性也实现了一个子集，包括新的原子内建函数、`CO_MIN`、`CO_MAX`、`CO_BROADCAST` 和 `CO_REDUCE` 集合操作（对多态和可分配组件类型有限制）、事件以及失败映像处理<sup>[8](https://gcc.gnu.org/onlinedocs/gfortran.pdf)</sup>。项目页面将当前状态描述为对 Fortran 2023 的初步支持<sup>[1](https://gcc.gnu.org/fortran/)</sup>。

`-std=level` 选项控制一致性检查。默认值 `gnu` 指定最新 Fortran 标准的超集，包含所有 GNU 扩展，并对过时扩展发出警告；`f95`、`f2003`、`f2008` 和 `f2018` 要求严格符合相应标准，`f2008ts` 则额外加入两份技术规范<sup>[9](https://flang.llvm.org/docs/OptionComparison.html)</sup>。

## 并行编程

**OpenMP 与 OpenACC。** GCC 的 GOMP 项目在 C、C++ 和 Fortran 前端中实现 OpenMP 和 OpenACC，包括相关的运行时库（libgomp，即 GNU 卸载与多处理运行时库）和 API 例程<sup>[10](https://gcc.gnu.org/projects/gomp/)</sup>。在 Fortran 中，`-fopenmp` 启用 `!$omp` 指令和 `!$` 条件编译哨兵，`-fopenacc` 在自由格式中启用 `!$acc` 指令，在固定格式中启用 `c$acc`、`*$acc` 和 `!$acc`；两个选项都隐含 `-pthread`，并安排运行时库的自动链接<sup>[11](https://gcc.gnu.org/onlinedocs/gcc/OpenMP-and-OpenACC-Options.html)</sup><sup> • </sup><sup>[4](https://gcc.gnu.org/onlinedocs/gfortran/OpenACC.html)</sup>。GNU Fortran 力求兼容 OpenACC 应用程序接口 v2.6<sup>[4](https://gcc.gnu.org/onlinedocs/gfortran/OpenACC.html)</sup>。启用任一选项时，编译器会为所有已配置的卸载设备类型自动生成卸载代码，可用 `-foffload=` 控制<sup>[10](https://gcc.gnu.org/projects/gomp/)</sup>。

**协数组。** 项目将支持使用 Fortran 协数组进行并行化列为其目标之一<sup>[1](https://gcc.gnu.org/fortran/)</sup>。在 GCC 16 发布系列中，协数组实现增加了对单节点机器上原生共享内存多线程的支持，以及对 Fortran 2018 `TEAM` 特性的处理<sup>[12](https://gcc.gnu.org/gcc-16/changes.html)</sup>。

## 架构与用法

GNU Fortran 是 GCC 内的一个前端：前端将源代码翻译为与语言无关的 GENERIC 形式，由共享的中间端进行优化，然后由后端为不同架构和操作系统生成代码。`gcc` 驱动程序对每个源文件调用 Fortran 前端程序 `f951`；单独的 `gfortran` 驱动程序与 `gcc` 完全相同，只是它会自动链接 Fortran 运行时库。GCC 将 `.f`、`.for`、`.ftn`、`.f90`、`.f95`、`.f03` 和 `.f08` 扩展名识别为 Fortran 源文件；小写固定格式扩展名按固定格式编译，`.f90` 一类扩展名按自由格式编译，大写版本则经过预处理<sup>[8](https://gcc.gnu.org/onlinedocs/gfortran.pdf)</sup>。

运行时库 libgfortran 包含支持编译阶段不直接生成的功能的机器码，例如内建函数和子程序，以及与文件和操作系统交互的例程<sup>[3](https://gcc.gnu.org/onlinedocs/gfortran/About-GNU-Fortran.html)</sup>。编译后的 Fortran 90 及以后版本的模块会生成 `.mod` 文件，描述模块的 API 和 ABI；这些文件因编译器和架构而异，每个 gfortran 版本都可能以不兼容的方式更改格式，因此 Fedora 等 Linux 发行版在 gfortran 更新时需要大规模重新构建 Fortran 软件包<sup>[13](https://fedoraproject.org/wiki/Packaging%3AFortran)</sup>。

对于混合语言程序，Fortran 2003 及以后版本的 C 绑定特性通常已经足够：Fortran 可以与 C++ 以及 C 混合使用，只需在 C++ 侧将接口函数声明为 `extern "C"`，在 Fortran 侧声明为 `BIND(C)`。`gfortran` 命令可以将 Fortran 与非 Fortran 代码链接在一起，也可以使用 `gcc`/`g++` 并显式加上 `-lgfortran` 选项<sup>[14](https://gcc.gnu.org/onlinedocs/gfortran/Mixed-Language-Programming.html)</sup>。

## 质量与性能

该编译器通过 NIST Fortran 77 测试套件，在 LAPACK 测试套件上产生可接受的结果，在 Polyhedron Fortran 编译器基准测试和 Livermore Fortran 内核测试中性能可观<sup>[3](https://gcc.gnu.org/onlinedocs/gfortran/About-GNU-Fortran.html)</sup>。在基准测试之外，它还编译过大型实际程序，包括 HARMONIE 和 [HIRLAM](https://www.edgepedia.org/zh/hirlam) 天气预报代码以及 Tonto 量子化学软件包<sup>[3](https://gcc.gnu.org/onlinedocs/gfortran/About-GNU-Fortran.html)</sup>。

与其他编译器的性能比较取决于代码和所链接的数学库。在 Linaro 于 AArch64 上使用 [SPEC CPU](https://www.edgepedia.org/zh/spec-cpu) 2017 进行的一项比较中，Fortran 基准的几何平均值显示 LLVM Flang 比 Classic Flang 慢约 48%，而 Classic Flang 总体比 Gfortran 慢 23%；同一比较发现，工具链之间的性能差异主要在于各自的 Fortran 前端，而非共享的后端优化<sup>[15](https://www.linaro.org/blog/comparing-llvm-flang-with-other-fortran-compilers)</sup>。在用户关于基于 LAPACK 的工作负载的报告中，归因于所链接的 BLAS/LAPACK 库而非编译器本身的差异，被认定为 gfortran 与 Intel 构建之间明显差距的常见原因<sup>[16](https://fortran-lang.discourse.group/t/why-is-the-intel-compiled-executable-that-much-faster-than-gnu/9947)</sup>。

## 最新发展

GNU Fortran 在 GCC 主线上开发，遵循开放开发流程，补丁由指定的维护者和贡献者小组审查<sup>[1](https://gcc.gnu.org/fortran/)</sup>。GCC 16 发布系列（GCC 16.1 于 2026 年 4 月 30 日发布；GCC 16.2 为错误修复版本，于 2026 年 8 月 7 日发布）增加了若干 Fortran 特性：改进的参数化派生类型支持、Fortran 2018 `IMPORT` 扩展、`REDUCE` 内建函数和新的 `GENERIC` 语句、Fortran 2023 三角函数增补如 `sinpi` 内建函数、`split` 内建子程序、`c_f_pointer` 的可选下界参数，以及一个 `-fexternal-blas64` 选项，用于在 `MATMUL` 中调用带 64 位整数参数的外部 BLAS 例程<sup>[12](https://gcc.gnu.org/gcc-16/changes.html)</sup><sup> • </sup><sup>[17](https://gcc.gnu.org/gcc-16/)</sup>。同一系列的 OpenACC 支持达到了 3.0 至 3.4 版本的部分要素，包括 `acc_memcpy_device` API 例程和数据子句中的 Fortran `PARAMETER` 常量<sup>[12](https://gcc.gnu.org/gcc-16/changes.html)</sup>。

## References

1. [GNU Fortran - GNU Project, gcc.gnu.org](https://gcc.gnu.org/fortran/)
2. [Introduction, The GNU Fortran Compiler (GCC 4.8.4 manual)](https://gcc.gnu.org/onlinedocs/gcc-4.8.4/gfortran/Introduction.html)
3. [About GNU Fortran, The GNU Fortran Compiler](https://gcc.gnu.org/onlinedocs/gfortran/About-GNU-Fortran.html)
4. [OpenACC, The GNU Fortran Compiler](https://gcc.gnu.org/onlinedocs/gfortran/OpenACC.html)
5. [Janus Weil, Re: Original commit history for gfortran, GCC fortran mailing list, June 2011](https://gcc.gnu.org/legacy-ml/fortran/2011-06/msg00142.html)
6. [Using GNU Fortran, GCC 4.2.4 manual (PDF)](https://gcc.gnu.org/onlinedocs/gcc-4.2.4/gfortran.pdf)
7. [Standards, The GNU Fortran Compiler](https://gcc.gnu.org/onlinedocs/gfortran/Standards.html)
8. [The GNU Fortran Compiler manual (PDF)](https://gcc.gnu.org/onlinedocs/gfortran.pdf)
9. [Compiler options comparison, The Flang Compiler documentation](https://flang.llvm.org/docs/OptionComparison.html)
10. [GNU Offloading and Multi-Processing Project (GOMP), gcc.gnu.org](https://gcc.gnu.org/projects/gomp/)
11. [OpenMP and OpenACC Options, Using the GNU Compiler Collection (GCC)](https://gcc.gnu.org/onlinedocs/gcc/OpenMP-and-OpenACC-Options.html)
12. [GCC 16 Release Series - Changes, New Features, and Fixes](https://gcc.gnu.org/gcc-16/changes.html)
13. [Packaging:Fortran, Fedora Project Wiki](https://fedoraproject.org/wiki/Packaging%3AFortran)
14. [Mixed-Language Programming, The GNU Fortran Compiler](https://gcc.gnu.org/onlinedocs/gfortran/Mixed-Language-Programming.html)
15. [Comparing LLVM Flang with other Fortran compilers, Linaro Blog, August 2023](https://www.linaro.org/blog/comparing-llvm-flang-with-other-fortran-compilers)
16. [Why is the Intel compiled executable that much faster than gnu?, Fortran Discourse, July 2025](https://fortran-lang.discourse.group/t/why-is-the-intel-compiled-executable-that-much-faster-than-gnu/9947)
17. [GCC 16 Release Series, gcc.gnu.org](https://gcc.gnu.org/gcc-16/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Compilers, interpreters, and toolchains › Compiler implementations

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/gnu-fortran

English original: https://www.edgepedia.org/gnu-fortran
