温馨提示×

ubuntu下fortran如何配置

小樊
37
2026-10-21 18:46:19
栏目: 智能运维

在 Ubuntu 下配置 Fortran 开发环境,通常步骤如下:


一、安装 Fortran 编译器

Ubuntu 下最常用的 Fortran 编译器是 gfortran(GNU Fortran)。

1. 更新软件源

sudo apt update

2. 安装 gfortran

sudo apt install gfortran

3. 验证安装

gfortran --version

看到版本信息即表示安装成功。


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

1. 创建源文件

nano hello.f90

内容示例(Fortran 90):

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

2. 编译

gfortran hello.f90 -o hello

3. 运行

./hello

三、常用编译选项

gfortran test.f90 -o test \
  -O2 \          # 优化
  -Wall \        # 显示警告
  -g \           # 调试信息
  -fcheck=all    # 运行时检查

四、使用 Makefile(推荐)

示例 Makefile:

FC = gfortran
FLAGS = -O2 -Wall

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

clean:
	rm -f test

使用:

make
./test
make clean

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

1. Intel Fortran(ifort,非必须)

需要安装 Intel oneAPI:

sudo apt install intel-oneapi-compiler-fortran

2. 调试工具

sudo apt install gdb

3. 数值库(如 LAPACK / BLAS)

sudo apt install liblapack-dev libblas-dev

编译时链接:

gfortran main.f90 -llapack -lblas -o main

六、编辑器推荐

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

如果你有具体需求(如:科学计算、并行 MPI、老旧 Fortran 77 代码、与 C 混合编程等),可以告诉我,我可以给你更针对性的配置方案。

0 踩