Prerequisites – Installing Homebrew and ASDF on Ubuntu
Documentation (Official?)
Popular Frameworks / Toolkits
(COBOL doesn’t use modern frameworks; these are the most common ones)
- GnuCOBOL (OpenCOBOL) — standard open-source compiler.
- TinyCOBOL — simple and educational.
- COBOL-IT — commercial, mainframe-compatible.
🛠️ Installation on Ubuntu
sudo apt update
sudo apt install open-cobol
# or
sudo apt install gnucobol
🍺 Installation with Homebrew
brew install gnu-cobol
📦 Standard Package Manager
COBOL **does not have an official package manager* (it’s not a modular ecosystem like Node or PHP).*
🔧 Installation with ASDF
There is no official ASDF plugin for COBOL.
📝▶️ Create and Run a COBOL File
Important: In COBOL, the first 7 characters of each line must remain empty because they were originally reserved for sequence numbers used on punched cards. These numbers allowed cards to be physically reordered if shuffled. Even though they’re no longer needed today, this historical structure is preserved for compatibility, so these first eight characters are still treated as a special non-code area.
cobol.es – IBM – Cobol for Linux
Col: 1 2 3 4 5 6 7 8
| | | |
| | | └──→ Code begins here
| | └────→ Indicator (comments, continuation…)
└──────────────→ Sequence area (1–6)
000100*······Sample program
000200 IDENTIFICATION DIVISION.
000300 PROGRAM-ID.··HELLOWORLD.
000400 ENVIRONMENT DIVISION.
000500 DATA DIVISION.
000600 WORKING-STORAGE SECTION.
000700 01·MESSAGE·PIC·X(20)·VALUE·"HELLO·WORLD".
000800 PROCEDURE DIVISION.
000900 DISPLAY·MESSAGE.
001000 STOP·RUN.
Note: There is a “free format” mode
>>SOURCE FORMAT FREEthat supposedly allows skipping the blank spaces, but it didn’t work for me.
Create file: touch hello.cob
*COBOL SAMPLE PROGRAM TO VERIFY COBOL INSTALLATION
*IMPORTANT - Reminder: COBOL code must begin at COLUMN 8.
*Use '*' in column 7 for comments.
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
DISPLAY "COBOL is installed and working!".
STOP RUN.
Same file in free format (no comments):
>>SOURCE FORMAT FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
DISPLAY "Hello COBOL World".
STOP RUN.
Compile:
cobc -x hello.cob -o hello
Run:
./hello
🟦 Basic COBOL Example
What It Does:
- Reads a text file.
- The file contains two numbers per line (e.g.,
5 7). - Multiplies them.
- Prints to console: "the result is = X"
Input file (datos.txt)
Example content:
125 745
313 246787
140 4467
Mini COBOL Program
Create file: multiplicar.cob
*-------------------------------------------------------------*
* PROGRAM TO READ TWO VALUES, MULTIPLY THEM, AND DISPLAY THEM *
*-------------------------------------------------------------*
IDENTIFICATION DIVISION.
PROGRAM-ID. MULTIPLY.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILEIN ASSIGN TO "datos.txt"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD FILEIN.
01 RECORD-LINE PIC X(50).
WORKING-STORAGE SECTION.
01 NUM1 PIC 9(9).
01 NUM2 PIC 9(9).
01 RESULT PIC 9(18).
01 END-FILE PIC X VALUE "N".
PROCEDURE DIVISION.
MAIN-LOGIC.
OPEN INPUT FILEIN
PERFORM UNTIL END-FILE = "S"
READ FILEIN
AT END
MOVE "S" TO END-FILE
NOT AT END
PERFORM PROCESS-LINE
END-READ
END-PERFORM
CLOSE FILEIN
STOP RUN.
PROCESS-LINE.
UNSTRING RECORD-LINE
DELIMITED BY SPACE
INTO NUM1 NUM2
COMPUTE RESULT = NUM1 * NUM2
DISPLAY "the result is = " RESULT.
📝▶️ How to Compile and Run
# Compile file:
cobc -x multiplicar.cob -o multiplicar
#Run compiled program
./multiplicar
Expected output:
the result is = 35
the result is = 6
the result is = 40
Top comments (1)
me voy a robar esto 😼