The use of Python ctypes to call COBOL

0 Likes

Problem

 error undefined symbol _mFsoload

 Resolution

This simple example demonstrates how well Visual COBOL can integrate with PYTHON.

It covers the core subjects of COBOL and C build, BASIC PYTHON scripting and execution.

Here is the output

Output

/home/tonyt/test/tmp >. ./doit.sh
COBDIR set to /home/products/vcdevhub22upd2preGA
cob64 -C nolist -Zv helloworld.c program1.cbl 
helloworld.c:
program1.cbl:
* Micro Focus COBOL                  V2.2 revision 002           Compiler
* Copyright (C) Micro Focus 1984-2014. All rights reserved.
* Accepted - verbose
* Accepted - nolist
* Compiling program1.cbl
* Total Messages:     0
* Data:         320     Code:          78
* Micro Focus COBOL Code Generator
* Copyright (C) Micro Focus 1984-2014. All rights reserved.
* Accepted - verbose
* Accepted - pic
* Generating program1
* Data:          88     Code:         544     Literals:          32

LD_LIBRARY_PATH     </home/products/vcdevhub22upd2preGA/lib>
LD_PRELOAD          </home/products/vcdevhub22upd2preGA/lib/libcobcrtn64.so:/home/products/vcdevhub22upd2preGA/lib/libcobrts64.so:/home/products/vcdevhub22upd2preGA/lib/libcobmisc64.so:/home/products/vcdevhub22upd2preGA/lib/libcobscreen64.so:/home/products/vcdevhub22upd2preGA/lib/libcobtrace64.so>
sys.path            <
['/home/tonyt/test/tmp', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib64/python2.6/site-packages/gst-0.10', '/usr/lib64/python2.6/site-packages/gtk-2.0', '/usr/lib64/python2.6/site-packages/webkit-1.0', '/usr/lib/python2.6/site-packages', '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info']
>

before c call
Started Hello World
program 1 has started
after c call

At the end of the output it shows Python calls, C then C calls COBOL, returning back to PYTHON.

The doit.sh script below shows displays from PYTHON, C and COBOL.

Here is the doit.sh

doit.sh

#
# set cobol environment
#
unset LD_LIBRARY_PATH
. /home/products/vcdevhub22upd2preGA/bin/cobsetenv
COBMODE=64
export COBMODE
#
# create c program
#
cat >helloworld.c <<EOF
/* Hello World program */

#include<stdio.h>

main()
{
    cobinit();
    printf("Started Hello World\n");
    program1();
    cobtidy();
}
EOF
#
# create cobol program
#
cat >program1.cbl <<EOF
       program-id. program1 as "program1".

       environment division.
       configuration section.

       data division.
       working-storage section.
       01 in_data        pic x(10) value "prog1 data".

       procedure division.

           display "program 1 has started"
      *     call "Program2" using in_data.
           
           goback.

       end program program1.

EOF
#
# build the .so file
#
cob -Zv helloworld.c program1.cbl
#
# create python script
#
cat >mytest.py <<EOF
import ctypes
import sys, os
print
print "LD_LIBRARY_PATH     <"   os.environ['LD_LIBRARY_PATH']   ">"
print "LD_PRELOAD          <"   os.environ['LD_PRELOAD']   ">"
print "sys.path            <" 
print sys.path 
print ">"
print
#
mylib = ctypes.CDLL("./libhelloworld.so")
print "before c call"
mylib.main()
print "after c call"
exit()
EOF
#
# run the python file
#
# get these modules loaded
#
export LD_PRELOAD=$COBDIR/lib/libcobcrtn64.so:$COBDIR/lib/libcobrts64.so:$COBDIR/lib/libcobmisc64.so:$COBDIR/lib/libcobscreen64.so:$COBDIR/lib/libcobtrace64.so
#
# run the python script
#
python mytest.py
#
# remove the LD_PRELOAD
#
unset LD_PRELOAD
#
# end
#

Create a tmp directory as in ouput above,

Enter the doit.sh code into a doit.sh script in the tmp directory,

Change the cobsetenv comand to point at your COBDIR and,

Execute the doit.sh to get it to work.

 

The key is the LD_PRELOAD for a simple c calling COBOL library

Comment List
Anonymous
Parents
  • The comments section of the Knowledge Base is the wrong place for an extended discussion. Please raise this as a new topic an appropriate MF Community forum.

    I'll be frank: It will be difficult to help you get this running if you don't have a solid understanding of basic Linux concepts. libdl.so is one such concept. So is the use of PATH and LD_LIBRARY_PATH. PATH is not used to search for shared objects, so there's no reason to be changing it. Adding random application directories (such as /Oracle/apps/...) to LD_LIBRARY_PATH is not likely to be productive; and now your script is resetting its entire value, so commenting out the unset command is pointless, and you've undone any changes that cobsetenv made to it.

Comment
  • The comments section of the Knowledge Base is the wrong place for an extended discussion. Please raise this as a new topic an appropriate MF Community forum.

    I'll be frank: It will be difficult to help you get this running if you don't have a solid understanding of basic Linux concepts. libdl.so is one such concept. So is the use of PATH and LD_LIBRARY_PATH. PATH is not used to search for shared objects, so there's no reason to be changing it. Adding random application directories (such as /Oracle/apps/...) to LD_LIBRARY_PATH is not likely to be productive; and now your script is resetting its entire value, so commenting out the unset command is pointless, and you've undone any changes that cobsetenv made to it.

Children
No Data
Related Discussions
Recommended