在Ubuntu下运行Fortran程序,可以按照以下步骤进行:
首先,确保你的系统上已经安装了Fortran编译器。最常用的编译器是gfortran。你可以使用以下命令在Ubuntu上安装gfortran:
sudo apt update
sudo apt install gfortran
使用文本编辑器(如nano
、vim
或gedit
)创建一个新的Fortran源文件。例如,创建一个名为hello.f90
的文件,并编写以下简单的Fortran代码:
program hello
implicit none
print *, 'Hello, World!'
end program hello
在终端中,导航到包含hello.f90
文件的目录,然后使用gfortran
编译器编译它:
gfortran -o hello hello.f90
这将生成一个名为hello
的可执行文件。
在终端中,使用以下命令运行编译后的Fortran程序:
./hello
你应该会看到输出 Hello, World!
。
如果在执行编译或运行命令时遇到权限问题,可以尝试使用sudo
运行命令:
sudo ./hello
为了方便使用Fortran编译器,可以将编译器的安装路径添加到系统的环境变量PATH
中。例如,如果gfortran安装在/usr/bin/gfortran
,可以编辑~/.bashrc
文件并添加以下行:
export PATH=$PATH:/usr/bin/gfortran
然后运行以下命令使更改生效:
source ~/.bashrc
通过以上步骤,你应该能够在Ubuntu上成功编译并运行Fortran程序。如果需要更高级的功能,可以考虑安装集成开发环境(IDE)如Visual Studio Code,并配置相应的Fortran插件。