Fortran/Hello world
簡単なFortranプログラム編集
以下は簡単なFortranのプログラムです。
- コンパイル・実行例
program hello print *, "Hello World!" end program
- 実行結果
Hello World!
Fortranは大文字と小文字を区別しないので、最初のプログラムは次のようにこともできます。
Program Hello
Print *, "Hello World!"
End Program
このプログラムでは、大文字小文字を区別されるのは、print文の引用符で囲まれた部分("Hello World!")だけです。
オンラインのコンパイル・実行環境 |
OnlineGDB の様な、オンラインのコンパイル・実行環境であればウェブブラウザーだけで、Fortranのプログラムをコンパイル・実行できます。 上の例の、コンパイル・実行例 のようなリンク先を開くと、ウェブブラウザー上でコンパイル・実行することが出来ます。 |
コンパイル・実行環境編集
上のコラムで紹介した「オンラインのコンパイル・実行環境」には、次のような特性があります。
- 長所
- ネットワーク環境とウェブブラウザーさえあればコンパイル・実行できる。
- コードの共有を URL を介してできる。
- 短所
- エディターなどの、使い慣れた開発ツールが使えない。
- 標準入力を使うプログラムの実行に向かない。
- 言語処理系の選択の幅が少ない(ない)
短所か問題になる場合は、自らコンパイル実行環境を構築することで解決する必要があります。
環境構築編集
gfortran編集
g95, flang などいくつかのオープン・ソース・ソフトウェアの Fortran 言語処理系がありますが、ここでは GNUプロジェクトのGCC(GNU Compiler Collection)のFortranフロントエンド gfortran を紹介します。
OnlineGDB でも、gfortran-4.8.5 が使われています。
- gfortran -v
Program Hello call system("gfortran -v") End Program
- 実行結果
Using built-in specs. COLLECT_GCC=gfortran COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.5-4ubuntu8~14.04.2' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.8.5 (Ubuntu 4.8.5-4ubuntu8~14.04.2)
GNU/Linux編集
ディストリビューションにより、パッケージマネージャとパッケージ体系に違いがありますが、
Fedora Linux では、
$ sudo dnf install gfortran
で導入できます。
Windows編集
いく通りかの方法がありますが、MSYS2環境にMinGW64の一部として、gfortran をインストールする方法を紹介します(MSYS2のインストール方法については、項を改めて紹介します)。
MSYS2/MinGW64のパッケージマネージャは、Arch Linux 由来の pacman(1) です。
- MINGW64
user@host MINGW64 ~ $ pacman -Syuu mingw32 is up to date mingw64 is up to date ucrt64 is up to date clang64 is up to date msys is up to date user@host MINGW64 ~ $ pacman -S base-devel mingw-w64-x86_64-toolchain
- プロンプトに MINGW64 があることを確認してください
- まず、パッケージシステムを最新にします(いくつか質問があるかもしれません)。
- base-devel と mingw-w64-x86_64-toolchain をインストールします。
- base-devel
- 基本的な開発用ユーティリティを集めたメタパッケージ
- asciidoc autoconf autoconf2.13 autogen automake-wrapper automake1.10 automake1.11 automake1.12 automake1.13 automake1.14 automake1.15 automake1.16 automake1.6 automake1.7 automake1.8 automake1.9 bison btyacc diffstat diffutils dos2unix file flex gawk gdb gettext gettext-devel gperf grep groff help2man texinfo texinfo-tex intltool libtool libunrar libunrar-devel unrar m4 make man-db pacman patch patchutils perl pkgconf pkgfile quilt reflex scons sed swig ttyrec wget xmlto
- mingw-w64-x86_64-toolchain
- MSYS2/MinGW64用のセルフ開発用ユーティリティを集めたメタパッケージ
- mingw-w64-x86_64-binutils mingw-w64-x86_64-crt-git mingw-w64-x86_64-gcc mingw-w64-x86_64-gcc-ada mingw-w64-x86_64-gcc-fortran mingw-w64-x86_64-gcc-libgfortran mingw-w64-x86_64-gcc-libs mingw-w64-x86_64-gcc-objc mingw-w64-x86_64-libgccjit mingw-w64-x86_64-gdb mingw-w64-x86_64-gdb-multiarch mingw-w64-x86_64-headers-git mingw-w64-x86_64-libmangle-git mingw-w64-x86_64-libwinpthread-git mingw-w64-x86_64-winpthreads-git mingw-w64-x86_64-make mingw-w64-x86_64-pkgconf mingw-w64-x86_64-tools-git mingw-w64-x86_64-winstorecompat-git
MinGW・MSYSの関係 |
今回はMSYS2をインストールして、 pacman を使ってツールチェーンを用意しました。 それぞれのソフトウェア群の開発用状況は日々変わりますので、現在(2021年8月)の状況と最新の状況は異なるかもしれません。 |
使い方・注意点編集
コンパイラーのバージョンの確認編集
環境が構築できたら、コンパイラーのバージョンを確認します。
- コンパイラーのバージョンの確認
$ gfortran -v
- 実行結果
user@hostname MINGW64 ~ Using built-in specs. COLLECT_GCC=C:\msys64\mingw64\bin\gfortran.exe COLLECT_LTO_WRAPPER=C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/lto-wrapper.exe Target: x86_64-w64-mingw32 Configured with: ../gcc-10.3.0/configure --prefix=/mingw64 --with-local-prefix=/mingw64/local --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --with-native-system-header-dir=/mingw64/x86_64-w64-mingw32/include --libexecdir=/mingw64/lib --enable-bootstrap --enable-checking=release --with-arch=x86-64 --with-tune=generic --enable-languages=c,lto,c++,fortran,ada,objc,obj-c++,jit --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-filesystem-ts=yes --enable-libstdcxx-time=yes --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-lto --enable-libgomp --disable-multilib --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-libiconv --with-system-zlib --with-gmp=/mingw64 --with-mpfr=/mingw64 --with-mpc=/mingw64 --with-isl=/mingw64 --with-pkgversion='Rev5, Built by MSYS2 project' --with-bugurl=https://github.com/msys2/MINGW-packages/issues --with-gnu-as --with-gnu-ld --with-boot-ldflags='-pipe -Wl,--dynamicbase,--high-entropy-va,--nxcompat,--default-image-base-high -Wl,--disable-dynamicbase -static-libstdc++ -static-libgcc' 'LDFLAGS_FOR_TARGET=-pipe -Wl,--dynamicbase,--high-entropy-va,--nxcompat,--default-image-base-high' --enable-linker-plugin-flags='LDFLAGS=-static-libstdc++\ -static-libgcc\ -pipe\ -Wl,--dynamicbase,--high-entropy-va,--nxcompat,--default-image-base-high\ -Wl,--stack,12582912' Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 10.3.0 (Rev5, Built by MSYS2 project) user@hostname MINGW64 ~ $
上の例は、Windows 11 上の MSYS2 / MinGW64 上の、gfortran-10.3.0です。環境によって gfortran のコマンド名に prefix や suffix か付く場合があります。
例えば、FreeBSDでは複数のバージョンのGCCパッケージが共存できるようバージョンごとにsuffixが付き gfortran10 の様なコマンド名になります。
コンパイルテスト編集
テキストエディタなどで hello.f90 を用意し、
- hello.f90
Program Hello Print *, "Hello World!" End Program
コマンドラインから、
gfortran hello.f90 -o hello
とすると、ソースコード hello.f90 から 実行形式 hello をコンパイルします(WinowsやOpenVMSでは、 .EXE の様な拡張子が暗黙に追加されるかもしれません)
gfortran ではデフォルトでFORTRAN 77規格を使用します(FORTRAN 77標準で使用されている固定形式のフォーマットが要求される)。
このため、自由形式のフォーマットを有効にするには、ソースファイルの拡張子を .f90
(またはそれ以降の規格)のにして、コンパイラが希望する規格を自動的に推測できるようにしなければなりません。ほかにも、コマンドラインから -ffree-form
オプションをコンパイラに渡すことでソースファイルの拡張子が .f
でも自由形式のフォーマットを有効にすることができます。
インデント |
(フリーフォーム記法の)Fortranの文法上はインデント(字下げ)は不要です。つまり program hello
print *,"Hello World!"
end program
のように記述しても実行可能です。しかし、インデントをしない書き方は、可読性が下がるので、あまり推奨できません。 |