在Ubuntu上测试Fortran代码,你可以遵循以下步骤:
sudo apt update
sudo apt install gfortran
.f90
(或其他Fortran 90/95/2003/2008等版本的扩展名)文件。例如,你可以创建一个名为hello.f90
的文件,并在其中编写简单的Fortran代码:program hello
print *, 'Hello, World!'
end program hello
cd /path/to/your/code
gfortran -o hello hello.f90
这将生成一个名为hello
的可执行文件(在Windows上将是hello.exe
)。
4. 运行Fortran程序:
在终端中,输入可执行文件的名称并按Enter键来运行程序。例如:
./hello
你应该会看到输出“Hello, World!”。 5. 调试和测试: 如果你的程序更复杂,你可能需要使用调试器(如gdb)来查找和修复错误。此外,你可以编写测试用例来验证程序的正确性。对于Fortran,你可以使用一些测试框架,如FRUIT(Fortran Unit Testing Interface Toolkit),来编写和运行单元测试。
注意:上述步骤适用于Fortran 90及更高版本。如果你使用的是更旧的Fortran版本(如Fortran 77),编译命令可能会有所不同。