温馨提示×

centos怎样写fortran脚本

小樊
45
2026-10-20 19:50:55
栏目: 智能运维

在 CentOS 上写并运行 Fortran 脚本(更准确说是 Fortran 程序),一般步骤如下:


一、安装 Fortran 编译器

CentOS 常用的是 gfortran(GNU Fortran)。

1. 安装 gfortran

sudo yum install gcc-gfortran

安装完成后检查:

gfortran --version

二、编写 Fortran 程序

Fortran 源文件通常以 .f、.f90、.f95 结尾。

示例:hello.f90

program hello
    implicit none
    print *, "Hello, CentOS Fortran!"
end program hello

用 vi 或 nano 创建文件:

vi hello.f90

三、编译 Fortran 程序

gfortran hello.f90 -o hello
  • hello.f90:源文件
  • -o hello:生成可执行文件 hello

四、运行程序

./hello

输出:

Hello, CentOS Fortran!

五、常见 Fortran 示例

1. 数值计算示例

program sum_example
    implicit none
    integer :: i, s
    s = 0
    do i = 1, 100
        s = s + i
    end do
    print *, "Sum =", s
end program sum_example

2. 读取输入

program input_example
    implicit none
    integer :: n
    print *, "Enter a number:"
    read *, n
    print *, "You entered:", n
end program input_example

六、常用编译选项

gfortran test.f90 -o test -O2      # 优化
gfortran test.f90 -o test -Wall    # 显示警告

七、补充说明

  • Fortran 不是“脚本语言”,而是编译型语言
  • CentOS 也支持 Intel Fortran(ifort),但需单独安装
  • 适合科学计算、数值模拟

如果你有 具体用途(如矩阵计算、调用数学库、并行 Fortran、Makefile 管理),我可以给你更针对性的示例。

0 踩