温馨提示×

ubuntu怎么配置fortran

小樊
38
2026-09-25 12:05:50
栏目: 智能运维

在 Ubuntu 上配置 Fortran 开发环境,通常就是安装 编译器(如 gfortran) 以及可选的 构建工具 / IDE。下面按步骤说明:


一、安装 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
./hello

输出:

Hello, Fortran!

三、常用编译选项

gfortran test.f90 -o test      # 基本编译
gfortran -O2 test.f90 -o test # 优化
gfortran -g test.f90 -o test  # 调试信息(配合 gdb)

四、使用 Makefile(推荐)

示例 Makefile:

FC = gfortran
FLAGS = -O2

test: test.f90
	$(FC) $(FLAGS) test.f90 -o test

clean:
	rm -f test

使用:

make
./test
make clean

五、安装其他工具(可选)

1. 调试工具

sudo apt install gdb

2. 数值库(如 BLAS / LAPACK)

sudo apt install liblapack-dev libblas-dev

编译时链接:

gfortran main.f90 -llapack -lblas -o main

3. IDE(可选)

  • VS Code + Fortran 插件
  • Code::Blocks
  • Emacs / Vim(配合语法插件)

六、常见问题

1. 找不到 gfortran

确保已执行:

sudo apt update

2. 老代码(.f 文件)

gfortran oldcode.f -o oldcode

如果你是想:

  • 配置 MPI Fortran
  • 使用 CMake
  • 配置 VS Code 开发环境
  • 安装 Intel Fortran(ifort)

可以告诉我,我可以给你更具体的配置方案。

0 踩