How do I use conditional compilation?


Question:

How do I use compiler directives to conditionally include or exclude sections of code?

Answer:

Here are some examples of conditional compilation:

EVALUATE and WHEN in a bug test program:

   id division.
   program-id. chinese.
   data division.
   working-storage section.
  >> EVALUATE CASE
  >> WHEN "TEST-1"
   77 chinese-item pic n(2) value nx"4EAC".
  >> WHEN "TEST-2"
   77 chinese-item pic n(1) value nx"4EAC".
  >> WHEN "TEST-3"
   77 chinese-item pic n(1) usage national.
  >> END-EVALUATE
   procedure division.
   main-logic.
       display standard graphical window.
       display chinese-item.
       accept omitted.
Then you can specify which variable to use by setting iscobol.compiler.const. You can set it in a compiler properties file:
   iscobol.compiler.const.CASE=TEST-1
or from the command line.
   iscc -Discobol.compiler.const.CASE=TEST-1 chinese.cbl
to include the TEST-1 case.

An alternative would be to add the following to the top of chinese.cbl:
   >> DEFINE CASE AS "TEST-3"
and write the program as:
   id division.
   program-id. chinese.
   data division.
   working-storage section.
  >> IF CASE IS = "TEST-1"
   77 chinese-item pic n(2) value nx"4EAC".
  >> ELSE 
  >> IF CASE IS = "TEST-2"
   77 chinese-item pic n(1) value nx"4EAC".
  >> ELSE
  >> IF CASE IS = "TEST-3"
   77 chinese-item pic n(1) usage national.
  >> END-IF
  >> END-IF
  >> END-IF
   procedure division.
   main-logic.
       display standard graphical window.
       display chinese-item.
       accept omitted.
In the above, CASE is a preprocessor constant set to either "TEST-1", "TEST-2" or "TEST-3".

The program could also be written to just test whether a constant is defined, as follows:
   id division.
   program-id. chinese.
   data division.
   working-storage section.
      >> IF TEST-1 IS DEFINED
   77 chinese-item pic n(2) value nx"4EAC".
      >> ELSE
      >> IF TEST-2 IS DEFINED
   77 chinese-item pic n(1) value nx"4EAC".
      >> ELSE
      >> IF TEST-3 IS DEFINED
   77 chinese-item pic n(1) usage national.
      >> END-IF
      >> END-IF
      >> END-IF
   procedure division.
   main-logic.
       display standard graphical window.
       display chinese-item.
       accept omitted.
and then compiled with this variable set in your compiler properties file:
   iscobol.compiler.const.TEST-1=on
or on your command line:
   iscc -Discobol.compiler.const.TEST-1=1 chinese.cbl
You can read more about the DEFINE, EVALUATE, and IF compiler directives in the isCOBOL SDK documentation, under the "Compiler and Runtime / Compiler / Compiler Directives" section

Article ID: 27
Created: August 28, 2009
Last Updated: August 4, 2022
Author: Support KB Author

Online URL: https://support.veryant.com/phpkb/article.php?id=27