Hello World#
在本教程的这一部分,我们将编写我们的第一个 Fortran 程序:无处不在的 "Hello, World!" 示例。
但是,在我们编写程序之前,我们需要确保我们已经设置了 Fortran 编译器。
Fortran 是一种 编译型语言 ,这意味着一旦编写,源代码必须通过编译器以生成可以运行的机器可执行文件。
编译器设置#
在本教程中,我们将使用免费和开源的 GNU Fortran 编译器 (gfortran),它是 GNU 编译器集合 (GCC) 的一部分。
To install gfortran on Linux, use your system package manager. On macOS, you can install gfortran using Homebrew or MacPorts. On Windows, you can download executable binaries.
要检查你是否正确设置了 gfortran,请打开终端并运行以下命令:
$> gfortran --version
这应该输出如下内容:
GNU Fortran 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Hello World#
设置好编译器后,在你喜欢的代码编辑器中打开一个新文件并输入以下内容:
program hello
! This is a comment line; it is ignored by the compiler
print *, 'Hello, World!'
end program hello
将程序保存到 hello.f90 后,在命令行编译:
$> gfortran hello.f90 -o hello
.f90是现代 Fortran 源文件的标准文件扩展名。 90 指的是 1990 年的第一个现代 Fortran 标准。
要运行你编译的程序:
$> ./hello
Hello, World!
恭喜,你已经编写、编译并运行了你的第一个 Fortran 程序!在本教程的下一部分中,我们将介绍用于存储数据的变量。