温馨提示×

ubuntu Fortran怎样配置环境

小樊
46
2026-10-01 07:39:10
栏目: 智能运维

在 Ubuntu 上配置 Fortran 开发环境,通常只需安装编译器及相关工具即可。下面按常见需求给你一套清晰步骤。


一、安装 Fortran 编译器(最常用:gfortran)

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

4. 运行

./hello

三、安装常用开发工具(可选但推荐)

1. 调试工具

sudo apt install gdb

2. 构建工具(如 CMake)

sudo apt install cmake

3. 编辑器 / IDE

  • VS Code(推荐)

    sudo snap install code --classic
    

    插件:Fortran Language Server、Modern Fortran

  • 命令行编辑器

    sudo apt install vim
    

四、使用 Intel Fortran(可选,性能更好)

如果你需要 ifort(Intel Fortran Compiler):

方法一:Intel oneAPI

  1. 官网下载: https://www.intel.com/content/www/us/en/developer/tools/oneapi/fortran-compiler.html

  2. 安装后配置环境:

source /opt/intel/oneapi/setvars.sh
  1. 使用:
ifort hello.f90 -o hello

五、常见路径与环境变量(一般不用改)

  • gfortran 路径:/usr/bin/gfortran
  • 库路径:/usr/lib/x86_64-linux-gnu

六、常见问题

1. 提示“command not found”

说明未安装或 PATH 未包含,重装 gfortran 即可。

2. 旧代码(.f 格式)

gfortran oldcode.f -o oldcode

如果你有 具体用途(如科学计算、数值模拟、MPI、OpenMP、VS Code 配置),可以告诉我,我可以给你更针对性的配置方案。

0 踩