温馨提示×

ubuntufortran环境怎么搭

小樊
48
2026-09-22 11:09:34
栏目: 智能运维

下面按从零开始的思路,给你一套在 Ubuntu 上搭建 Fortran 开发环境 的常用方案,适合学习和科学计算。


一、安装 Fortran 编译器(最基础)

Ubuntu 上最常用的是 gfortran(GNU Fortran)。

1. 更新软件源

sudo apt update

2. 安装 gfortran

sudo apt install gfortran

3. 验证安装

gfortran --version

能看到版本号就说明成功了。


二、写第一个 Fortran 程序

1. 创建文件

nano hello.f90

2. 输入代码

program hello
    print *, "Hello, Fortran!"
end program hello

3. 编译运行

gfortran hello.f90 -o hello
./hello

三、推荐的开发工具

1. 文本编辑器(轻量)

sudo apt install vim
# 或
sudo apt install code   # VS Code(需先装官网 deb)

VS Code 推荐插件:

  • Fortran
  • Modern Fortran

四、科学计算常用库(可选)

1. BLAS / LAPACK(线性代数)

sudo apt install liblapack-dev libblas-dev

编译时链接:

gfortran main.f90 -llapack -lblas

2. MPI(并行计算)

sudo apt install mpich

示例:

mpif90 hello.f90 -o hello
mpirun -np 4 ./hello

3. CMake(工程化管理)

sudo apt install cmake

五、进阶:Intel Fortran(可选)

如果你做高性能计算,可以用 Intel oneAPI:

wget https://registrationcenter-download.intel.com/.../l_fortran-compiler...
bash l_fortran-compiler*.sh

(配置较复杂,一般科研用 gfortran 足够)


六、常见问题

❓ 编译报错:命令找不到

→ 没装 gfortran 或没刷新终端

❓ 老代码 .f 编译不了

gfortran old.f -ffixed-form

七、总结(最简环境)

sudo apt update
sudo apt install gfortran

就够了 ✅


如果你告诉我:

  • Ubuntu 版本
  • 用途(课程 / 科研 / 工程)
  • 是否用 MPI / OpenMP / 数值库

我可以帮你定制一套更合适的 Fortran 环境。

0 踩