在Ubuntu中构建Fortran项目可以通过以下几种方式进行:
安装gfortran: 打开终端并运行以下命令来安装gfortran编译器及其所需的标准库。
sudo apt update
sudo apt install gfortran
编写和编译Fortran代码:
hello.f90
:program hello
implicit none
print *, "Hello, World!"
end program hello
gfortran hello.f90 -o hello
./hello
安装Intel Fortran编译器:
编写和编译Fortran代码:
hello.f90
:program hello
implicit none
print *, "Hello, World!"
end program hello
ifort -o hello hello.f90
./hello
安装FPM:
fpm-0.9.0-linux-x86_64
文件重命名为fpm2
。fpm
的可执行文件路径添加到PATH
中。export PATH=$PATH:/path/to/fpm2
~/.bashrc
文件,将上述命令添加到文件末尾,然后运行source ~/.bashrc
。创建和运行FPM项目:
mytest
的新项目:fpm new mytest
cd mytest
。fpm run
命令来构建和运行项目。fpm run
如果你需要安装特定版本的Fortran编译器,例如gfortran-7
,可以使用以下命令:
sudo apt install gfortran-7
然后,你可以使用以下命令来切换默认的gfortran
版本:
sudo update-alternatives --config gfortran
根据提示选择你想要的默认版本。
通过以上步骤,你可以在Ubuntu上成功构建和运行Fortran项目。选择合适的编译器和构建工具,根据项目需求进行编译,可以有效地提高开发效率。