在 Ubuntu 上学习 Fortran,主要分三步:安装编译器 → 写第一个程序 → 系统学习语法与工具。下面给你一条清晰、可操作的路线。
Ubuntu 自带软件源里有 gfortran,直接用 apt 安装:
sudo apt update
sudo apt install gfortran
安装完成后检查版本:
gfortran --version
能看到版本号就说明成功了。
mkdir fortran_study
cd fortran_study
nano hello.f90
写入以下内容(现代 Fortran,Free Format):
program hello
implicit none
print *, "Hello, Fortran!"
end program hello
保存退出(Ctrl+O,回车,Ctrl+X)。
gfortran hello.f90 -o hello
./hello
输出:
Hello, Fortran!
重点掌握:
program / end program)integer, real, character, logical)implicit none 的重要性read *, print *)if, do, select case)示例:
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
function)和子程序(subroutine)module)示例(函数):
function square(x) result(y)
implicit none
real :: x, y
y = x * x
end function square
Fortran 常用于:
Ubuntu 可安装:
sudo apt install liblapack-dev libopenmpi-dev
| 用途 | 工具 |
|---|---|
| 编辑器 | VS Code(装 Fortran 插件)、Vim |
| 调试 | gdb |
| 构建 | make、CMake |
| 文档 | doxygen |
VS Code 插件搜索:
你可以告诉我:
我可以直接给你针对性示例和练习。