TL; DR
- GCC15をインストールすると
gcobol-15
コマンドが使えるようになる - ubuntu25.04ではaptでインストール可能
$ gcobol-15 hello.cbl -o hello
$ ./hello
HELLO WORLD!!
はじめに
2025年4月にGCC15がリリースされ、COBOLがコンパイルできるようになりました。
本記事では、実行方法や触ってみた所感について軽く紹介したいと思います。
(COBOL初心者なので誤りがあればコメントいただけるとありがたいです)
インストール方法
面倒なのでDockerイメージを使用しました。Ubuntu25.04からaptでGCC 15がインストールできるので、apt install gcobol-15
でインストールします。
FROM ubuntu:25.04
RUN apt-get update -y &&\
apt-get install -y gcobol-15
services:
cobol:
build:
context: .
volumes:
- .:/work
tty: true
コンテナに入るとコマンドが使えることが確認できます。
$ docker compose up -d --build
$ docker compose exec cobol bash
root@c91442e12d56:/work# gcobol-15 -v
Driving: (3)
[ 0] 2454 gcobol-15
[ 1] 2444 -v
[ 2] 2328 -rdynamic
Using built-in specs.
COLLECT_GCC=gcobol-15
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-linux-gnu/15/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 15-20250404-0ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-15/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2,rust,cobol,algol68 --prefix=/usr --with-gcc-major-version-only --program-suffix=-15 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/libexec --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-libstdcxx-backtrace --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-15-n9L2J0/gcc-15-15-20250404/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-15-n9L2J0/gcc-15-15-20250404/debian/tmp-gcn/usr --enable-offload-defaulted --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 15.0.1 20250404 (experimental) [master r15-9193-g08e803aa9be] (Ubuntu 15-20250404-0ubuntu1)
コンパイル
gcobol-15 $src -o $target
で直接実行ファイルを作成できます。
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
DISPLAY 'HELLO WORLD!!'.
STOP RUN.
$ gcobol-15 hello.cbl -o hello
$ ./hello
HELLO WORLD!!
また、 -S
を付けることでアセンブリを出力することも可能です。
# コンパイルしアセンブリhello.sを生成
$ gcobol-15 hello.cbl -S
使えなかった機能
画面機能は未対応
CUIを作ろうと思っていたのですが、SCREEN SECTION
には未対応でした。
gcobol does not implement Report Writer or Screen Section.
現状 SCREEN SECTION
を含むソースコードはコンパイル時にクラッシュします。
$ gcobol-15 simple_screen.cbl -o simple_screen
(null):0: confused by earlier errors, bailing out
データ型にそぐわない初期値が入っているとクラッシュ
VALUE
で文字列定数を指定すべきところに数値を指定すると、実行時にクラッシュしてしまいました。
identification division.
program-id. comparez.
data division.
working-storage section.
* valueには文字列 "1" を指定する必要がある
* (もしくは、Zではなく9を使用する)
01 num pic Z value 1.
procedure division.
if num > 0
display "larger";
end-if
stop run.
$ gcobol-15 comparez.cbl -o comparez
$ ./comparez
Unknown conversion 11 in format_for_display_internal
GNU COBOLで実行したところ以下の警告が発生し、原因を特定できました。
.code.tio: 8: warning: alphanumeric value is expected
実行時にクラッシュした際は別の処理系を使ってソースコードに不備が無いか調べるのが良さそうです。