在Ubuntu上运行Fortran程序,可以按照以下步骤进行:
安装Fortran编译器
首先,确保你已经安装了Fortran编译器。在Ubuntu上,可以使用包管理器安装GFortran编译器。打开终端并运行以下命令:
sudo apt update sudo apt install gfortran
编写Fortran程序
创建一个简单的Fortran程序文件,例如hello.f90
,并添加以下代码:
program hello print *, "Hello, World!" end program hello
编译Fortran程序
在终端中,导航到包含hello.f90
文件的目录,然后运行以下命令来编译程序:
gfortran hello.f90 -o hello
这将生成一个名为hello
的可执行文件。
运行编译后的Fortran程序
在终端中,输入以下命令并按Enter键来运行编译后的程序:
./hello
如果一切正常,终端将输出:
Hello, World!
使用Fortran程序包管理器(fpm)
还介绍了一个名为fpm的Fortran程序包管理器和构建系统,它可以简化Fortran程序或库的构建、测试和分发过程。
- 安装fpm:
wget https://github.com/fortran-lang/fpm/releases/download/v0.9.0/fpm-0.9.0-linux-x86_64 sudo mv fpm-0.9.0-linux-x86_64 fpm2 export PATH=$PATH:/path/to/fpm2
注意将/path/to/fpm2
替换为实际的文件路径。
- 创建和运行fpm项目:
fpm new mytest cd mytest fpm run
这将编译并运行名为mytest
的项目。
通过这些步骤,你应该能够在Ubuntu上成功运行Fortran程序。如果需要更复杂的编译和构建流程,可以考虑使用fpm等工具来简化操作。