HOME  NEWS  FORUM  DOWNLOAD  LINK
OpenCOBOL - an open-source COBOL compiler
Main Menu
Download
Documentation
Development
Who's Online
22 user(s) are online (4 user(s) are browsing Wiki)

Members: 0
Guests: 22

more...
Powered by
SourceForge

Xoops

Creative Commons

Top / UserManual / 2_3


C Interface

This chapter describes how to combine C programs with COBOL programs.

Main C Program

Include libcob.h in your C program. Call cob_init before using any COBOL module:

#include <libcob.h>

int
main(int argc, char **argv)
{
  /* initialize your program */
  ...

  /* initialize the COBOL run-time library */
  cob_init(argc, argv);

  /* rest of your program */
  ...
}

You can write cobc_init(0, NULL); if you do not want to pass command line arguments to COBOL.

The function cob_set_library_path will overwrite the user setting of the environment variable COB_LIBRARY_PATH.

You can compile your C program as follows:

$ cc -c `cob-config --cflags` main.c

The compiled object must be linked with libcob as follows:

$ cc -o main main.o `cob-config --libs`

Static C to COBOL

Let's call the following COBOL module from a C program:

---- say.cob ---------------------------
      IDENTIFICATION DIVISION.
      PROGRAM-ID. say.
      ENVIRONMENT DIVISION.
      DATA DIVISION.
      LINKAGE SECTION.
      01 HELLO PIC X(6).
      01 WORLD PIC X(6).
      PROCEDURE DIVISION USING HELLO WORLD.
          DISPLAY HELLO WORLD.
          EXIT PROGRAM.
----------------------------------------

Remember that the code has to start at column 9.

This program accepts two arguments, displays them, and exit.

From the viewpoint of C, this is equivalent to a function having the following prototype:

extern int say(char *hello, char *world);

So, your main program will look like as follows:

---- hello.c ---------------------------
#include <stdio.h>
#include <libcob.h>

extern int say(char *hello, char *world);

int
main()
{
  int ret;
  char hello[6] = "Hello ";
  char world[6] = "World!";

  cob_init(0, NULL);

  ret = say(hello, world);

  return ret;
}
----------------------------------------

Compile these programs as follows:

$ cc -c `cob-config --cflags` hello.c
$ cobc -c -static say.cob
$ cobc -x -o hello hello.o say.o
$ ./hello
Hello World!

Dynamic C to COBOL

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 <stdio.h>
#include <stdlib.h>
#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 -x -o hello hello-dynamic.o
$ cobc -m say.cob
$ export COB_LIBRARY_PATH=.
$ ./hello
Hello World!

Static COBOL to C

Let's call the following C function from COBOL:

---- say.c -----------------------------
#include <stdio.h>
int
say(char *hello, char *world)
{
  int i;
  for (i = 0; i < 6; i++)
    putchar(hello[i]);
  for (i = 0; i < 6; i++)
    putchar(world[i]);
  putchar('\n');
  return 0;
}
----------------------------------------

This program is equivalent to the foregoing say.cob.

Note that, unlike C, the arguments passed from COBOL programs are not terminated by the null character (i.e., \0).

If you want to call it in native WIN environments (doesn't hurt if you do this in every case) the code must be changed to

---- say.c -----------------------------
#include <stdio.h>
#ifndef C_EXTERN
#if defined (_WIN32) || defined (__CYGWIN__)
#define C_EXTERN __declspec(dllexport) extern
#else
#define C_EXTERN extern
#endif
#endif
C_EXTERN int
say(char *hello, char *world)
{
  int i;
  for (i = 0; i < 6; i++)
    putchar(hello[i]);
  for (i = 0; i < 6; i++)
    putchar(world[i]);
  putchar('\n');
  return 0;
}
----------------------------------------

You can call this function in the same way you call COBOL programs:

---- hello.cob -------------------------
      IDENTIFICATION DIVISION.
      PROGRAM-ID. hello.
      ENVIRONMENT DIVISION.
      DATA DIVISION.
      WORKING-STORAGE SECTION.
      01 HELLO PIC X(6) VALUE "Hello ".
      01 WORLD PIC X(6) VALUE "World!".
      PROCEDURE DIVISION.
          CALL "say" USING HELLO WORLD.
          STOP RUN.
----------------------------------------

Compile these programs as follows:

$ cc -c say.c
$ cobc -c -static -fmain hello.cob  => since OC 1.0 : not to do (fmain is deprecated cf. NEWS file)
$ cobc -x -o hello hello.o say.o    => since OC 1.0 : $ cobc -x -o hello hello.cob say.o
$ ./hello
Hello World!

Dynamic COBOL to C

You can create a dynamic-linking module from a C program by passing an option -shared to the C compiler:

$ cc -shared -o say.so say.c
$ cobc -x -o hello hello.cob
$ export COB_LIBRARY_PATH=.
$ ./hello
Hello World!


Front page   Edit Freeze Diff Backup Upload Copy Rename Reload   New List of pages Search Recent changes   Help   RSS of recent changes
Counter: 34571, today: 14, yesterday: 16
Last-modified: Wed, 24 Mar 2010 08:23:48 GMT (1703d)
Copyright (C) 2005 The OpenCOBOL Project. All rights reserved.
Powered by Xoops2 | PHP | MySQL | Apache
ocean-net