在 CentOS 上写并运行 Fortran 脚本(更准确说是 Fortran 程序),一般步骤如下:
CentOS 常用的是 gfortran(GNU Fortran)。
sudo yum install gcc-gfortran
安装完成后检查:
gfortran --version
Fortran 源文件通常以 .f、.f90、.f95 结尾。
program hello
implicit none
print *, "Hello, CentOS Fortran!"
end program hello
用 vi 或 nano 创建文件:
vi hello.f90
gfortran hello.f90 -o hello
hello.f90:源文件-o hello:生成可执行文件 hello./hello
输出:
Hello, CentOS Fortran!
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
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、Makefile 管理),我可以给你更针对性的示例。