在Linux下编译和运行Fortran程序,你需要安装一个Fortran编译器,比如gfortran。以下是详细步骤:
使用包管理器安装:
sudo apt update
sudo apt install gfortran
sudo dnf install gcc-gfortran
sudo pacman -S gfortran
验证安装: 安装完成后,可以通过以下命令检查gfortran版本:
gfortran --version
假设你有一个名为hello.f90的Fortran源文件,内容如下:
program hello
print *, 'Hello, World!'
end program hello
你可以使用以下命令编译这个程序:
gfortran -o hello hello.f90
这会生成一个名为hello的可执行文件。
编译成功后,可以通过以下命令运行程序:
./hello
你应该会看到输出:
Hello, World!
编译多个源文件:如果你有多个源文件,比如main.f90和utils.f90,可以这样编译:
gfortran -o myprogram main.f90 utils.f90
启用优化:可以使用-O选项来启用优化,比如:
gfortran -O2 -o myprogram main.f90 utils.f90
调试信息:可以使用-g选项来包含调试信息:
gfortran -g -o myprogram main.f90 utils.f90
静态链接:可以使用-static选项来进行静态链接:
gfortran -static -o myprogram main.f90 utils.f90
通过这些步骤,你应该能够在Linux下顺利编译和运行Fortran程序。