COBOL 環境構築 で困っていませんか?
- 書籍の手順通りにやってもエラーが出る
- そもそも情報が少なくて何を参考にすればいいか分からない
- 自分の環境(Mac / Windows)での手順が見つからない
私も同じ経験をしました。この記事では、私が実際に構築した手順を公開します。
Contents
この記事を書いた経緯
私の主な作業端末はWindowsです。当初は自分用にWindows(WSL)での環境構築手順を確立しました。
後日、後輩から「COBOL環境構築でつまずいた」と相談を受けました。話を聞くと、後輩の個人端末はMacでした。
たまたま私もMacを持っていたので、Mac環境での手順も試行錯誤しながら確立。後輩に共有したところ、無事に環境構築できたと喜んでくれました。
せっかくなので、Mac・Windows両方の手順をこの記事にまとめて公開することにしました。先駆者の方々の記事に助けられた恩返しも込めて。
前提条件
この記事の手順を進めるにあたり、以下を準備してください。
- VS Code(インストール済み)
※VS Codeのインストール方法も必要であれば、追記を検討します。
この記事の進め方
この記事に記載しているコマンドは、コピペで進められるようにしています。
ただし、各コマンドの実行結果は必ず確認してください。途中でエラーが出ていても気づかずに進めると、後で「なぜか動かない」となりがちです。
初心者の方でも確認できるよう、私の実行結果も載せています。自分の結果と見比べながら、一つずつ確実に進めてください。
Mac編:Homebrew + GnuCOBOL
Mac環境では、Homebrewを使ってGnuCOBOLをインストールします。
参考にした記事
- https://qiita.com/mikomokaru-jpn/items/e60f44e9d9a2fcb01dcb
- https://qiita.com/glassmonkey/items/695e0b021cdcb3092b98
1. Homebrewインストール
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
インストール完了後、表示される「Next steps」のコマンドを実行してPATHを通します。
echo >> ~/.zprofile
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
確認:
brew -v
実行結果(例):
Homebrew 5.0.7
バージョンが表示されれば成功です。
2. GnuCOBOLインストール
brew install gnucobol
確認:
cobc -V
実行結果(例):
cobc (GnuCOBOL) 3.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
...
バージョンが表示されれば成功です。
3. 動作確認(HelloWorld)
helloWorld.cbl を作成:
IDENTIFICATION DIVISION.
PROGRAM-ID. SAMPLE-01.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
MAIN.
DISPLAY "Hello world!" UPON CONSOLE.
STOP RUN.
ビルド:
cobc -x helloWorld.cbl
実行結果:
(出力なし)
エラーが出なければ成功です。helloWorld という実行ファイルが生成されます。
確認:
ls -al
実行結果(例):
-rwxr-xr-x 1 user staff 34520 helloWorld
-rw-r--r-- 1 user staff 358 helloWorld.cbl
実行:
./helloWorld
実行結果:
Hello world!
Windows編:WSL + opensourceCobol
Windows環境では、WSL(Windows Subsystem for Linux)を使ってopensourceCobolをインストールします。
参考にした記事
- https://qiita.com/nor51010/items/f11beed7a9a24bf3035e
- https://qiita.com/nor51010/items/d2f1cb46304eff0291de
1. opensourceCobol取得
git clone https://github.com/opensourcecobol/opensource-cobol.git
実行結果(例):
Cloning into 'opensource-cobol'...
remote: Enumerating objects: 2947, done.
remote: Counting objects: 100% (46/46), done.
...
Resolving deltas: 100% (2255/2255), done.
確認:
ls
実行結果:
opensource-cobol
2. 必要なパッケージインストール
sudo apt-get install build-essential
sudo apt-get install libncurses5 libncurses5-dev
sudo apt-get install libgmp-dev
sudo apt-get install bison flex
sudo apt-get install gettext
sudo apt-get install automake autoconf
sudo apt install make
sudo apt install gcc
※各コマンドで Do you want to continue? [Y/n] と表示されたら Y を入力してください。
3. VBISAM 構築・導入
cd ~/opensource-cobol/vbisam
./configure
make
sudo make install
確認:
ls /usr/local/bin/
実行結果(例):
vbcheck vbrecover
vbcheck、vbrecover が表示されれば成功です。
4. opensourceCobol 構築・導入
cd ~/opensource-cobol
./configure --with-vbisam
make
sudo make install
確認:
ls /usr/local/bin/
実行結果(例):
cob-config cobc cobcrun vbcheck vbrecover
cobc、cobcrun が表示されれば成功です。
5. 動作確認(HelloWorld)
作業フォルダ作成:
mkdir -p ~/cobol
cd ~/cobol
HELLO.cbl を作成:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
PROCEDURE DIVISION.
MAIN-RTN.
DISPLAY "HELLO WORLD!".
MAIN-EXT.
STOP RUN.
ビルド:
cobc HELLO.cbl
実行結果:
(出力なし)
エラーが出なければ成功です。
実行:
cobcrun HELLO
実行結果:
HELLO WORLD!
以下のエラーが出た場合:
cobcrun: error while loading shared libraries: libcob.so.1: cannot open shared object file
原因は外部ライブラリ(libcob.so)の参照パスが未設定のためです。
解決方法:
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
恒久的に設定する場合は ~/.bashrc に追記して source ~/.bashrc を実行してください。
VS Code デバッグ環境構築
VS Codeでビルド・デバッグができるように設定します。
1. 拡張機能インストール
以下の拡張機能をインストールしてください。
- C/C++(Microsoft)
- COBOL Language Support(Broadcom)
2. gdb インストール(WSLの場合)
sudo apt-get install gdb
確認:
whereis gdb
実行結果(例):
gdb: /usr/bin/gdb /etc/gdb /usr/include/gdb /usr/share/gdb /usr/share/man/man1/gdb.1.gz
/usr/bin/gdb が表示されればインストール成功です。
3. 作業フォルダ作成
WSL側:
mkdir -p ~/cobol/helloworld
cd ~/cobol/helloworld
VS Codeを起動:
code .
4. c_cpp_properties.json
Ctrl + Shift + P → C/C++: Edit Configurations (JSON) を選択し、以下の内容に編集:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"browse": {
"path": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}
5. tasks.json
Ctrl + Shift + P → Tasks: Configure Default Build Task → Create tasks.json file from template → Others を選択し、以下の内容に編集:
{
"version": "2.0.0",
"windows": {
"options": {
"shell": {
"executable": "c:\\Windows\\System32\\bash.exe",
"args": ["-c"]
}
}
},
"tasks": [
{
"label": "build cobol hello world on WSL",
"type": "shell",
"command": "cobc",
"args": [
"-g",
"--save-temp=${workspaceFolder}",
"-o",
"${workspaceFolder}/HELLO.so",
"HELLO.cbl"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
6. launch.json
Run and Debug → create a launch.json file をクリックし、以下の内容に編集:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) cobol Launch",
"type": "cppdbg",
"request": "launch",
"program": "/usr/local/bin/cobcrun",
"args": ["HELLO"],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"windows": {
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
"pipeTransport": {
"pipeCwd": "",
"pipeProgram": "c:\\windows\\sysnative\\bash.exe",
"pipeArgs": ["-c"],
"debuggerPath": "/usr/bin/gdb"
},
"sourceFileMap": {
"/mnt/c": "${env:systemdrive}/"
}
}
]
}
7. デバッグ実行
HELLO.cblを作成(Windows編で作成したもの)Ctrl + Shift + Bでビルド- 生成された
HELLO.cの該当行にブレークポイントを設定 F5でデバッグ開始
ブレークポイントで止まれば成功です。
まとめ
環境構築お疲れさまでした。
私自身、環境構築に週末2週分かかりました。正直、心が折れそうになったこともあります。
でも、振り返ると環境構築はゴールではなくスタートでした。
環境ができたら、次は実際にCOBOLのコードを書いて学習するフェーズです。私も環境構築後、書籍を使ってCOBOLの基礎を学びました。
学習方法やおすすめ書籍については、別記事で詳しく紹介しています。
→ COBOL 入門|未経験から始める学習方法と環境構築のポイント
参考にした記事
この記事を書くにあたり、以下の先駆者の方々の記事を参考にしました。ありがとうございました。
Mac編:
- https://qiita.com/mikomokaru-jpn/items/e60f44e9d9a2fcb01dcb
- https://qiita.com/glassmonkey/items/695e0b021cdcb3092b98
Windows編: