在Ubuntu上使用Fortran模块需按以下步骤操作:
安装Fortran编译器:
sudo apt update
sudo apt install gfortran
验证安装:gfortran --version
。
编写模块代码:
创建模块文件(如my_module.f90
),定义数据、子程序等:
module my_module
implicit none
integer, parameter :: my_param = 42
contains
subroutine my_subroutine(x)
integer, intent(in) :: x
print *, 'Value:', x
end subroutine
end module
```。
编写主程序并调用模块:
创建主程序文件(如main.f90
),用use
语句引入模块:
program main
use my_module
implicit none
call my_subroutine(my_param)
end program
```。
编译代码:
将模块与主程序一起编译,指定模块路径(若不在当前目录):
gfortran -o main my_module.f90 main.f90
# 若模块在特定路径,添加 -I 选项
# gfortran -o main -I/path/to/modules my_module.f90 main.f90
```。
运行程序:
./main
```。
说明:
-I
选项指定。-L
和-l
选项。