Node:Dynamic C to COBOL, Next:, Previous:Static C to COBOL, Up:C Interface



Dynamic linking with COBOL programs

You can find a COBOL module having a specific PROGRAM-ID by using a C function cob_resolve, which takes the module name as a string and returns a pointer to the module function.

cob_resolve returns NULL if there is no module. In this case, the function cob_resolve_error returns the error message.

Let's see an example:

     ---- hello-dynamic.c -------------------
     #include <libcob.h>
     
     static int (*say)(char *hello, char *world);
     
     int
     main()
     {
       int ret;
       char hello[6] = "Hello ";
       char world[6] = "World!";
     
       cob_init(0, NULL);
     
       /* find the module with PROGRAM-ID "say". */
       say = cob_resolve("say");
     
       /* if there is no such module, show error and exit */
       if (say == NULL) {
         fprintf(stderr, "%s\n", cob_resolve_error ());
         exit(1);
       }
     
       /* call the module found and exit with the return code */
       ret = say(hello, world);
     
       return ret;
     }
     ----------------------------------------
     

Compile these programs as follows:

     $ cc -c `cob-config --cflags` hello-dynamic.c
     $ cobc -o hello hello-dynamic.o
     $ cobc -m say.cob
     $ export COB_LIBRARY_PATH=.
     $ ./hello
     Hello World!