Compiling from the command line

To compile a COBOL program from the command line, issue the cob2 command. This command also invokes the linker.

cob2 command syntax

Read syntax diagramSkip visual syntax diagramcob2optionsfilenames

To compile multiple files, specify the file-names at any position in the command line, using spaces to separate options and file-names. For example, the following two commands are equivalent:


cob2 -g filea.cbl fileb.cbl -q"flag(w)"
cob2 filea.cbl -g -q"flag(w)" fileb.cbl 

The cob2 command accepts compiler and linker options in any order on the command line. Any options that you specify apply to all files on the command line.

Only source files that have suffix .cbl or .cob are passed to the compiler. All other files are passed to the linker.

The default location for compiler input and output is the current directory.

The cob2 command is thread safe. cob2_r is provided for compatibility with COBOL for AIX®, but uses the same default options as the cob2 command. See Modifying the default compiler configuration for more information on default options.

Note: The compiler interprets certain shell scripting characters as follows:
  • An equal sign (=) is interpreted to a left parenthesis, (
  • A colon (:) is interpreted to a right parenthesis, )
  • An underscore (_) is interpreted to a single quotation mark (')

You can add a backslash (\) escape character to prevent the interpretation and thus to pass characters in the strings. If you want the backslash (\) to represent itself (rather than as an escape character), use the double backslash (\\).

Examples: using cob2 for compiling

Related references  
cob2 options  
Compiler options