温馨提示×

ubuntufortran怎么配置

小樊
38
2026-09-22 11:07:33
栏目: 智能运维

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


一、安装 Fortran 编译器(gfortran)

1. 更新软件源

sudo apt update

2. 安装 gfortran

sudo apt install gfortran

3. 验证安装

gfortran --version

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


二、编写并编译一个 Fortran 程序

示例:hello.f90

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

编译

gfortran hello.f90 -o hello

运行

./hello

三、常用编译选项

gfortran hello.f90 -o hello \
  -O2 \          # 优化
  -Wall \        # 显示警告
  -g             # 生成调试信息

四、安装开发辅助工具(可选)

1. 调试器

sudo apt install gdb

2. 构建工具(如 CMake)

sudo apt install cmake

3. 编辑器 / IDE

  • VS Code(推荐,安装 Fortran 插件)
  • vim / emacs
  • Code::Blocks(支持 Fortran)

五、使用 Makefile(示例)

FC = gfortran
FLAGS = -O2 -Wall

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

clean:
	rm -f hello

使用:

make
./hello

六、常见问题

1. 找不到 gfortran

确认是否已安装:

which gfortran

2. 老代码(.f 文件)

直接用 gfortran 也能编译:

gfortran oldcode.f -o oldcode

如果你是想:

  • 配置 MPI Fortran
  • 使用 Intel Fortran (ifort)
  • 在 Ubuntu Server / WSL 上配置
  • 做 科学计算(LAPACK / BLAS)

可以告诉我你的具体需求,我可以给你更针对性的配置方案。

0 踩