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

Members: 0
Guests: 20

more...
Powered by
SourceForge

Xoops

Creative Commons

Top / UserManual / 2_2


Multiple Sources

A program often consists of multiple source files. This section describes how to compile multiple source files.

This section also describes how to build a shared library that can be used by any COBOL programs and how to use external libraries from COBOL programs.

Static Linking

The easiest way of combining multiple files is to compile them into a single executable.

Compile each file with the option -c, and link them at the end. The top-level program must be compiled with the option -fmain:

$ cobc -c subr1.cob
$ cobc -c subr2.cob
$ cobc -c -fmain main.cob
$ cobc -o prog main.o subr1.o subr2.o

You can link C routines as well:

$ cc -c subrs.c
$ cobc -c -fmain main.cob
$ cobc -o prog main.o subrs.o

Any number of functions can be contained in a single C file.

The linked programs will be called dynamically; that is, the symbol will be resolved at run time. For example, the following COBOL statement

CALL "subr" USING X.

will be converted into an equivalent C code like this:

int (*func)() = cob_resolve("subr");
if (func != NULL)
  func (X);

With the compiler options -fstatic-call, more efficient code will be generated like this:

subr(X);

Note that this option is effective only when the called program name is a literal (like CALL "subr".). With a data name (like CALL SUBR.), the program is still called dynamically.

Dynamic Linking

The main program and subprograms can be compiled separately.

The main program is compiled as usual:

$ cobc -x main.cob

Subprograms are compiled with the option -m:

$ cobc -m subr.cob

This creates a module file subr.so (The extension varies depending on your host.).

Before running the main program, install the module files in your library directory:

$ cp subr.so /your/cobol/lib

Now, set the environment variable COB_LIBRARY_PATH to your library directory, and run the main program:

$ export COB_LIBRARY_PATH=/your/cobol/lib
$ ./main

Building Library

You can build a shared library by combining multiple COBOL programs and even C routines:

$ cobc -c subr1.cob
$ cobc -c subr2.cob
$ cc -c subr3.c
$ cc -shared -o libsubrs.so subr1.o subr2.o subr3.o

Using Library

You can use a shared library by linking it with your main program.

Before linking the library, install it in your system library directory:

$ cp libsubrs.so /usr/lib

or install it somewhere else and set LD_LIBRARY_PATH:

$ cp libsubrs.so /your/cobol/lib
$ export LD_LIBRARY_PATH=/your/cobol/lib

Then, compile the main program, linking the library as follows:

$ cobc main.cob -L/your/cobol/lib -lsubrs


Front page   Edit Freeze Diff Backup Upload Copy Rename Reload   New List of pages Search Recent changes   Help   RSS of recent changes
Counter: 31296, today: 6, yesterday: 10
Last-modified: Tue, 15 Jan 2008 05:13:49 GMT (2381d)
Copyright (C) 2005 The OpenCOBOL Project. All rights reserved.
Powered by Xoops2 | PHP | MySQL | Apache
ocean-net