-M
Purpose
Use the -M compiler option to automatically generate dependency information that can be used in a makefile.
Syntax
Defaults
By default, the compiler does not generate dependency information.
Usage
With this option, GNU make can automatically recompile a COBOL program if its source or any of the copybooks it uses are modified. This improves developer productivity by allowing developers to do iterative, incremental builds, while also ensuring that programs are always built with the latest version of any copybooks that they use.
See the GNU make (gmake) documentation for details on how to use this feature in a makefile at Generating Prerequisites Automatically.
Example 1: generating dependency information with -M
main.cbl file that includes two programs where each include a
copybook, and you compile it with the following
command:cob2 -c -I./copybooks -M main.cbl, it will generate the following
message:Generating dependencies...
Compiling...
IBM COBOL for Linux 1.2.0 compile started
End of compilation 1, program PGM1, no statements flagged.
End of compilation 2, program PGM2, no statements flagged.
main.d file will be created containing a list of
dependencies and the compiler return code will be 0. Below is an example of the
main.d file:main.o: main.cbl \
/home/username/copybook1.cbl \
/home/username/./copybooks/copybook2.cbl \
/home/username/copybook1.cbl \
/home/username/./copybooks/copybook2.cbl
In the preceding example there were two programs in main.cbl and they both included the same copybook. Therefore main.d shows two entries for the same copybooks. Any duplicates will be disregarded by the GNU make utility.
If during dependency generation the compiler return code is greater than the value of
-comprc_ok, compiler messages will be output and the compilation will exit because
the generation of dependencies is considered to have failed.
Example 2: building COBOL applications incrementally while managing dependencies with a makefile
COB2 = /usr/bin/cob2
COB2FLAGS = -I./copybooks -M
# The source files that will be compiled and then linked to produce
# the prog1 executable:
PROG1SOURCE = prog1a.cbl prog1b.cbl
# The source files that will be compiled and then linked to produce
# the prog2 executable:
PROG2SOURCE = prog2a.cbl prog2b.cbl
PROG1OBJECTS := $(patsubst %.cbl, %.o, $(PROG1SOURCE))
PROG2OBJECTS := $(patsubst %.cbl, %.o, $(PROG2SOURCE))
ALLOBJECTS := $(PROG1OBJECTS) $(PROG2OBJECTS)
all: prog1 prog2
# Link to produce prog1:
prog1: $(PROG1OBJECTS)
$(COB2) -o $@ $+
# Link to produce prog2:
prog2: $(PROG2OBJECTS)
$(COB2) -o $@ $+
# The rule that tells gmake what to do when you compile a .cbl file.
# Since COB2FLAGS has the -M option in it, the compiler will produce
# not only a .o file but also a .d file containing dependency information
# naming the copybooks used in each .cbl file.
%.o : %.cbl
$(COB2) $(COB2FLAGS) -c $< -o $@
# When a .cbl file is compiled with the -M option as above, -M produces a file with
# the .d extension. Such files are included in the makefile here so gmake knows
# that a .cbl file must be recompiled if any of the copybooks the .cbl file uses
# have changed, not just when the .cbl file itself changes.
-include $(patsubst %.o,%.d,$(ALLOBJECTS))
