A Makefile for several programs
One source file is one command, and a job of two programs and a module is three commands that have to run in the right order. make keeps the order, skips the work that is already done and knows what a change to one file costs.
Two programs and a module
The two programs do the same small thing and both call the same subprogram, which prints the date and the time. That shared subprogram is the reason for the build file: it is compiled once and linked into both programs.
The first program is report.cob: above the listing stand IDENTIFICATION DIVISION. and PROGRAM-ID. REPORT., and the listing starts at the procedure division because the program calls a subprogram that lives in another file, which the checker of this course cannot put beside it.
PROCEDURE DIVISION.
DISPLAY "report of the job".
CALL "STAMP".
STOP RUN.
END PROGRAM REPORT.
The second is load.cob, the same shape with its own line, and PROGRAM-ID. LOAD. above the listing:
PROCEDURE DIVISION.
DISPLAY "load of the job".
CALL "STAMP".
STOP RUN.
END PROGRAM LOAD.
IDENTIFICATION DIVISION.
PROGRAM-ID. STAMP.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NOW PIC X(21).
PROCEDURE DIVISION.
MOVE FUNCTION CURRENT-DATE TO WS-NOW.
DISPLAY "run at " WS-NOW(1:8) " " WS-NOW(9:6).
GOBACK.
END PROGRAM STAMP.
The Makefile
Save it as Makefile. The recipe lines start with a tab, not with spaces, which is the one rule of the format that a text editor can break silently:
COBC = cobc
FLAGS = -x -free -Wall
all: report load
report: report.cob stamp.o
$(COBC) $(FLAGS) -o report report.cob stamp.o
load: load.cob stamp.o
$(COBC) $(FLAGS) -o load load.cob stamp.o
stamp.o: stamp.cob
$(COBC) -c -free -Wall -o stamp.o stamp.cob
clean:
rm -f report load stamp.o
.PHONY: all clean
Three targets are files and one is a name. report and load each depend on their own source and on stamp.o, so a change to the stamp rebuilds both. clean is declared .PHONY because a directory called clean would otherwise make it look done.
make and make again
The first run does the three commands in the order of the dependencies, and the second has nothing to do:
make
make
cobc -c -free -Wall -o stamp.o stamp.cob
cobc -x -free -Wall -o report report.cob stamp.o
cobc -x -free -Wall -o load load.cob stamp.o
make: Nothing to be done for 'all'.
The order is the lesson. make all wants report, which wants stamp.o, so the object is compiled before the first link and the second program reuses it. A Makefile that lists the links first would fail on the first run and succeed on the second, which is the worst kind of build file.
The two programs run and print the same stamp, because the subprogram was called twice within one second:
./report
./load
report of the job
run at 20261118 112706
load of the job
run at 20261118 112706
A rebuild after one change
Touching the shared source is enough to bring the whole build back, because the object is newer than before and both programs depend on it:
touch stamp.cob
make
cobc -c -free -Wall -o stamp.o stamp.cob
cobc -x -free -Wall -o report report.cob stamp.o
cobc -x -free -Wall -o load load.cob stamp.o
Three commands again, and no more. A change to report.cob would bring one command, because load does not depend on it, and that is the difference between a build file and a shell script of three commands: the script runs all three every time.
The files it leaves are 20304 bytes for report, 20296 for load and 6344 for the object, so each executable holds its own copy of the subprogram. The lesson on modules is what to do when that copy is the thing you want to avoid.
Exercises
- Add a third program to the Makefile and to all. Which lines does make run on a clean tree?
- Build stamp.cob as a module with -m instead of an object and change the two link lines. What does make run now?
- Touch report.cob only and read the lines make prints.
- Run make clean and then make -n. What does the dry run print?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does make do when its targets are newer than their sources?
Answer
Nothing, and it says so
The second make printed Nothing to be done for all.
-
Which character has to start a recipe line in a Makefile?
Answer
A tab
A recipe with spaces instead of a tab is the classic broken Makefile.
-
Why is stamp.o compiled before the two links?
Answer
Both programs depend on it, and make builds a dependency first
The three commands of the first make run in that order.
-
What does touching the shared source cost?
Answer
The object and both links, three commands
The rebuild after the touch ran the same three commands.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-N PIC 9(2) VALUE 3. 01 W-T PIC 9(3) VALUE 0. PROCEDURE DIVISION. PERFORM VARYING W-N FROM 1 BY 1 UNTIL W-N IS GREATER THAN 3 ADD W-N TO W-T END-PERFORM. DISPLAY W-T. STOP RUN.Answer
006
The loop counts one, two and three into the total.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC X(6) VALUE "OBJECT". PROCEDURE DIVISION. DISPLAY "[" FUNCTION TRIM(W-A) "]". STOP RUN.Answer
[OBJECT]
The six letters fill the field and the trim changes nothing.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC 9(3) VALUE 100. 01 W-B PIC 9(3) VALUE 200. 01 W-T PIC 9(4). PROCEDURE DIVISION. ADD W-A W-B GIVING W-T DISPLAY W-T. STOP RUN.Answer
0300
A hundred and two hundred make three hundred.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-N PIC 9(4) VALUE 0. 01 W-I PIC 9(2) VALUE 0. PROCEDURE DIVISION. PERFORM VARYING W-I FROM 1 BY 2 UNTIL W-I IS GREATER THAN 5 ADD W-I TO W-N END-PERFORM. DISPLAY W-N. STOP RUN.Answer
0009
The counter runs 1, 3 and 5, which add to nine.
-
What is the difference between a Makefile of three commands and a script of three commands?
Answer
The Makefile runs only what a change made stale
The second make did nothing and the touch brought back exactly three commands.
-
Why is clean declared .PHONY?
Answer
So a file or folder named clean cannot make make think the target is done
A phony target is a name, not a file.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and make on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev