- 定义COBOL程序
> cat TEST.cbl
IDENTIFICATION DIVISION.
PROGRAM-ID. TEST.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SEND-STRING PIC X(100) VALUE SPACES.
LINKAGE SECTION.
01 CMD-PARAM.
03 CMD-LENGTH PIC 9(4) COMP-X.
03 CMD-LINE.
05 CMD-CHAR PIC X
OCCURS 1 TO 999 DEPENDING ON CMD-LENGTH.
******************************************************
* Start program with command line args
******************************************************
PROCEDURE DIVISION USING CMD-PARAM.
MOVE CMD-LINE TO SEND-STRING.
* ACCEPT SEND-STRING FROM COMMAND-LINE.
DISPLAY "SEND-STRING:" SEND-STRING.
STOP RUN.
- 配置setting
为了支持上述linkage section从命令行读取参数,必须
- 定义COBOL配置文件,包含如下行
> cat cobconfig.cfg
set command_line_linkage=TRUE
- 设置环境变量
SET COBCONFIG=/path/to/cobconfig.cfg
如果觉得配置setting比较繁琐,也可以在COBOL程序里面指定Directive COMMAND-LINE-LINKAGE
,例如:
> cat TEST.cbl
$SET COMMAND-LINE-LINKAGE
IDENTIFICATION DIVISION.
PROGRAM-ID. TEST.
DATA DIVISION.
...
- 编译运行
>cbllink -V -K TEST.cbl
...
>TEST.exe abcd
SEND-STRING:abcd
- 编译运行:分步执行
前面使用cbllink直接从.cbl编译成了.exe,也可以分两步从.cbl->.obj->.exe.
>cobol TEST.cbl;
>cbllink -V -K TEST.obj
注意cobol命令后面的分号(;)不能少,它不是分隔符,而是命令的一部分哦。
这里再细分一下,cbllink又包含两个子命令:
> cblnames -v -mTEST TEST.obj
> cl TEST.obj cbllds.obj cblrtsmi.lib oopsm.lib msvcrt.lib kernel32.lib ucrt.lib vcruntime.lib legacy_stdio_definitions.lib legacy_stdio_wide_specifiers.lib kernel32.lib user32.lib gdi32.lib advapi32.lib /link /SUBSYSTEM:CONSOLE /ENTRY:mainCRTStartup /OUT:TEST.exe
其中cbllds.obj是由命令cblnames生成的。
网友评论