在 Ubuntu 上配置 Fortran 开发环境其实很简单,核心就是安装 Fortran 编译器(最常用的是 gfortran)。下面按步骤说明。
sudo apt update
sudo apt install gfortran
gfortran --version
看到类似 GNU Fortran (Ubuntu ...) x.x.x 就说明安装成功。
nano hello.f90
写入以下内容(Fortran 90 语法):
program hello
print *, "Hello, Fortran!"
end program hello
保存并退出(Ctrl + O,Enter,Ctrl + X)。
gfortran hello.f90 -o hello
./hello
输出:
Hello, Fortran!
gfortran test.f90 -o test # 基本编译
gfortran -O2 test.f90 -o test # 开启优化
gfortran -g test.f90 -o test # 生成调试信息(配合 gdb)
gfortran -Wall test.f90 -o test # 显示警告
sudo apt install gdb
sudo apt install libopenblas-dev liblapack-dev
sudo apt install mpich
编译:
mpif90 mpi_test.f90 -o mpi_test
示例 Makefile:
FC = gfortran
FLAGS = -O2
hello: hello.f90
$(FC) $(FLAGS) hello.f90 -o hello
clean:
rm -f hello
使用:
make
./hello
make clean
确认是否已安装:
which gfortran
若无输出,重新执行安装命令。
文件扩展名可用 .f:
gfortran oldcode.f -o oldcode
如果你是想:
可以告诉我你的具体用途,我可以给你更针对性的配置方案。