How to compile COBOL program into shared library

本文介绍如何将COBOL和C语言程序混合编译为单一共享库,并通过C程序调用。示例包括使用COBOL-IT编译器创建共享库libmylib.so的过程,以及主要执行文件mypgm如何加载并调用共享库中的函数。

This sample describe how compile mixed COBOL and C program into a single shared library, and used by C program.


1. HELLOCBL.cbl: provide a COBOL function

2. HELLOCPP.c : provide a C function

3. mypgm.cc: the major executable file

1 and 2 is compiled into a shared library libmylib.so



COBOL-IT compiler is used.


HELLOCBL.cbl

       IDENTIFICATION   DIVISION.
       PROGRAM-ID.      HELLOCBL.
       ENVIRONMENT      DIVISION.
       INPUT-OUTPUT     SECTION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
         01  ARG        PIC X(50) VALUE SPACES.
       LINKAGE SECTION.
         01  PARM-SIZE  PIC S9(8) COMP-5.
         01  PARM-DATA  PIC X(256).
       PROCEDURE DIVISION USING PARM-SIZE PARM-DATA.
           DISPLAY "HELLOCBL PARM-SIZE=[" PARM-SIZE "]".
           DISPLAY "HELLOCBL PARM-DATA=[" PARM-DATA(1:PARM-SIZE) "]".
           MOVE 2 TO RETURN-CODE.
           GOBACK.
      *    STOP RUN.



HELLOCPP.c

//extern "C"
int HELLOCPP(int i, char * buffer) {
    printf("HELLOCPP PARM-SIZE=[%d]\n", i);
    printf("HELLOCPP PARM-DATA=[%s]\n", buffer);
    return 1;
}



mypgm.cpp

#include <string.h>
#include <dlfcn.h>

#include <libcob.h>

typedef int (* func_t)(int, char *);

void * loadHandle(const char * library) {
    void * handle = dlopen(library, RTLD_LAZY | RTLD_GLOBAL);
    char * errmsg = dlerror();
    if (errmsg != NULL) {
        printf("Cannot load shared library: [%s]: [%s]", library, errmsg);
        return NULL;
    }
    if (handle == NULL) {
        printf("Cannot load shared library: [%s]", library);
        return NULL;
    }
    return handle;
}

func_t loadFunction(void * handle, const char * name) {
    func_t func = (func_t) dlsym(handle, name);
    if (func == 0) {
        printf("Cannot get dlsym: [%s]", name);
        return NULL;
    }
    return func;
}

void callcppfunc(void * handle) {
    func_t funcpp = loadFunction(handle, "HELLOCPP");
    if (funcpp != NULL) {
        int ret = funcpp(5, "1234567890");
        printf("HELLOCPP return %d\n", ret);
    }
}

void callcblfunc(int argc, char * argv[]) {
    cit_runtime_t * rtd = cob_get_rtd();
    cob_init(rtd, argc, argv);

    int size = 5;
    char buffer[] = { "1234567890" };
    void *  cobargv[3];
    cobargv[0] = (void *)&size;
    cobargv[1] = (void *)buffer;
    cobargv[2] = NULL;

    int ret = cobcall("HELLOCBL", 2, cobargv);
    printf("HELLOCBL return %d\n", ret);
    cob_rtd_tidy(rtd);
}

int main(int argc, char *argv[])
{
    const char * library = "libmylib.so";
    void * handle = loadHandle(library);
    if (handle != NULL) {
        callcppfunc(handle);
        callcblfunc(argc, argv);
    }
    return 0;
}


makefile

PROGRAM=mypgm
LIBRARY=libmylib.so

all: $(PROGRAM) $(LIBRARY)

mypgm: mypgm.cpp
        g++ $< -g -o $@ -I$(COBDIR)/include -L$(COBDIR)/lib -lcobit -ldl

$(LIBRARY): HELLOCBL.cbl HELLOCPP.c
        cobc -fno-C-cmd-line -Wlinkage -b -g -v -fixed $^ -o $(@F) -std=mf -fmf-gnt -constant "UPPER-CALL=1"

clean:
        -rm -rf c *.lst *.lis *.int *.idy $(LIBRARY) $(PROGRAM)



Execution

$ make
$ ./mypgm 
HELLOCPP PARM-SIZE=[5]
HELLOCPP PARM-DATA=[1234567890]
HELLOCPP return 1
HELLOCBL PARM-SIZE=[+0000000005]
HELLOCBL PARM-DATA=[12345]
HELLOCBL return 2


the end

To link a C++ shared library, you need to follow these steps: 1. Compile your C++ code into an object file using the `-c` flag. For example, if your C++ code is in a file called `mylibrary.cpp`, you would run the following command: `g++ -c -fPIC mylibrary.cpp` The `-fPIC` flag is necessary to generate Position Independent Code, which is needed for shared libraries. 2. Link the object file into a shared library using the `-shared` flag. For example, if your object file is called `mylibrary.o`, you would run the following command: `g++ -shared -o libmylibrary.so mylibrary.o` The `-o` flag specifies the output file name, and the `.so` extension is used for shared libraries on Linux. 3. Include the shared library in your project by specifying the library name and location in your build system. For example, if you are using CMake, you would add the following lines to your `CMakeLists.txt` file: ``` add_library(mylibrary SHARED IMPORTED) set_target_properties(mylibrary PROPERTIES IMPORTED_LOCATION /path/to/libmylibrary.so) ``` This tells CMake to import the `mylibrary` shared library and set its location to `/path/to/libmylibrary.so`. 4. Use the functions and classes defined in the shared library in your code by including the appropriate header files and linking against the library. For example, if your library defines a function called `myfunction`, you would include its header file and link against the library like this: ``` #include "mylibrary.h" ... myfunction(); ``` And in your build system, you would specify the library as a dependency: ``` target_link_libraries(myprogram mylibrary) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值