Mixed Languages Compile AND Link (Cobol calling C++)

Hello Experts,

I am trying to call a C++ program from a Cobol program. I have searched all over for 3.5 weeks, and this is driving me crazy. I am running CYGWIN on a windows 7 machine. My UNIX is very novice.

My c++ program computes a sha256 hash algorithm. I know it works, as i have successfully called it from another C++ program, and verified the results.

My problem is that when I try to dynamically link the main cobol program with the C++ sub program, everything looks fine on the surface. But when I execute the main program, and it calls the C++ program, I get the message displayed on the console: libcob: Cannot find module 'SUBC'.

Here are the steps I followed to compile the main program and link it with the C++ subprogram, as per OpenCobol

1.) $ cobc -x CALLSHA.cbl --- Compile the calling program
2.) $ g++ -shared -o SUBC.so SUBC.cpp --- Compile C++ sub program
3.) $ cp SUBC.so /Lib --- install the module in library directory
4.) $ export COB_LIBRARY_PATH=/Lib ---
5.) $ ./CALLSHA --- Run main program which is dynamically linked with C++ module

HERE IS THE OUTPUT FROM THE RUN:

CALL from main. --- Message produced in main program right before the call
libcob: Cannot find module 'SUBC' --- Error message says C++ program not found
Hi kbw,

Yes, I have been using those as a guide. The thing is, those compile examples are in C, not C++.

The second thing is that, my main program is a cobol program that calls a C++ program. The second link you posted is the reverse of that.

I realize that you have to use the g++ compiler instead of CC.

When I tried to compile and LINK the 2 programs "Statically", I was getting errors, even though compiling them seperately was no problem.

When I compiled and Linked them "Dynamically", I was able to get it compiled and Linked, but when the program is run, the c++ module when called by Cobol gives the error: libcob: Cannot find module 'SUBC'.

I will post the code a little later, along with the compile and link steps. I have tried many things. I never thought I would have this much trouble finding an example of a Cobol Program Calling a C++. But then again, as I said straight away, I am not a Unix guru.



HERE IS THE MAIN COBOL PROGRAM...

IDENTIFICATION DIVISION.
Program-ID. GetHash.
AUTHOR. JOHN LAURENCE GLACKEN.
ENVIRONMENT DIVISION.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 IO-PARM.
05 I-PARM PIC X(214).
05 I-PARM-RED REDEFINES I-PARM.
07 POS_SET PIC 9(10).
07 TREE_01 PIC 9(10).
07 TREE_02 PIC 9(10).
07 TREE_03 PIC 9(10).
07 TREE_04 PIC 9(10).
07 TREE_05 PIC 9(10).
07 TREE_06 PIC 9(10).
07 TREE_07 PIC 9(10).
07 TREE_08 PIC 9(10).
07 TREE_09 PIC 9(10).
07 TREE_10 PIC 9(10).
07 TREE_11 PIC 9(10).
07 TREE_12 PIC 9(10).
07 FACT_01 PIC X(07).
07 FACT_02 PIC X(07).
07 FACT_03 PIC X(07).
07 FACT_04 PIC X(07).
07 FACT_05 PIC X(07).
07 FACT_06 PIC X(07).
07 FACT_07 PIC X(07).
07 FACT_08 PIC X(07).
07 FACT_09 PIC X(07).
07 FACT_10 PIC X(07).
07 FACT_11 PIC X(07).
07 FACT_12 PIC X(07).
05 O-PARM PIC X(64).
01 proc-pointer procedure-pointer.
PROCEDURE DIVISION.
MOVE "0000000001" TO POS_SET, TREE_01, TREE_02.
MOVE "0000000000" TO TREE_03, TREE_04, TREE_05, TREE_06,
TREE_07, TREE_08, TREE_09.
MOVE "0000000000" TO TREE_10, TREE_11, TREE_12.
MOVE "0000000" TO FACT_01, FACT_02, FACT_03, FACT_04,
FACT_05, FACT_06, FACT_07, FACT_08,
FACT_09.
MOVE "AVAMTU " TO FACT_10.
MOVE "AVAMTL " TO FACT_11.
MOVE "AVAMT" TO FACT_12.
DISPLAY 'CALL from main.'.

CALL "GetSha256Hash" USING I-PARM.
DISPLAY 'BACK FROM CALLING sha256ZZ'.
STOP RUN.

***************************************************************

Basically, I am passing a string of 214 characters to the GetSha256Hash method in the C++ sub program, which expects a string, and returns the hash.
Last edited on
And here is the C++ Sub program (Note: I change the name from SUBC, and is now called GetSha256Hash)


#include <stdio.h>
#include<iostream>
#include<vector>
#include<fstream>
#include<exception>
#include<string>
#include <cstdlib>
#ifndef C_EXTERN
#if defined (_MSC_VER) || defined (__BORLANDC__) || ( defined (WIN32) && defined (__GNUC__) )
#define C_EXTERN __declspec(dllexport)
#else
#define C_EXTERN extern
#endif
#endif

typedef unsigned int uint;



using namespace std;
C_EXTERN string fromDecimal(uint n, int b)
{
string chars="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string result="";
while(n>0)
{
result=chars.at(n%b)+result;
n/=b;
}

return result;
}

uint K[]=
{ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};

C_EXTERN void makeblock(vector<uint>& ret, string p_msg)
{
uint cur=0;
int ind=0;
for(uint i=0; i<p_msg.size(); i++)
{
cur = (cur<<8) | (unsigned char)p_msg[i];

if(i%4==3)
{
ret.at(ind++)=cur;
cur=0;
}
}
}

class Block
{
public:
vector<uint> msg;

Block():msg(16, 0) { }


Block(string p_msg):msg(16, 0)
{
makeblock(msg, p_msg);
}

};

C_EXTERN void split(vector<Block>& blks, string& msg)
{
for(uint i=0; i<msg.size(); i+=64)
{

try
{
makeblock(blks[i/64].msg, msg.substr(i, 64));
}
catch(...)
{
cout<<"exc";
}
}
}

C_EXTERN string mynum(uint x)
{
string ret;
for(uint i=0; i<4; i++)
ret+=char(0);

for(uint i=4; i>=1; i--) //big endian machine assumed
{
ret += ((char*)(&x))[i-1];
}
return ret;
}

#define shr(x,n) ((x & 0xFFFFFFFF) >> n)
#define rotr(x,n) (shr(x,n) | (x << (32 - n)))

C_EXTERN uint ch(uint x, uint y, uint z)
{
return (x&y) ^ (~x&z);
}

C_EXTERN uint maj(uint x, uint y, uint z)
{
return (x&y) ^ (y&z) ^ (z&x);
}

C_EXTERN uint fn0(uint x)
{
return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22);
}

C_EXTERN uint fn1(uint x)
{
return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25);
}

C_EXTERN uint sigma0(uint x)
{
return rotr(x, 7) ^ rotr(x, 18) ^ shr(x, 3);
}

C_EXTERN uint sigma1(uint x)
{
return rotr(x, 17) ^ rotr(x, 19) ^ shr(x, 10);
}


C_EXTERN string GetSha256Hash(string Args)

{
string Sha256HashValue, msg_arr, msg;
msg_arr=Args;
msg=msg_arr;
msg_arr += (char)(1<<7);
uint cur_len = msg.size()*8 + 8;
uint reqd_len = ((msg.size()*8)/512+1) *512;
uint pad_len = reqd_len - cur_len - 64;

string pad(pad_len/8, char(0));
msg_arr += pad;
string len_str(mynum(msg.size()*8));
msg_arr = msg_arr + len_str;

uint num_blk = msg_arr.size()*8/512;
vector<Block> M(num_blk, Block());
split(M, msg_arr);

uint H[]={0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};

for(uint i=0; i<num_blk; i++)
{
vector<uint> W(64, 0);
for(uint t=0; t<16; t++)
{
W[t] = M[i].msg[t];
}

for(uint t=16; t<64; t++)
{
W[t] = sigma1(W[t-2]) + W[t-7] + sigma0(W[t-15]) + W[t-16];
}

uint work[8];
for(uint i=0; i<8; i++)
work[i] = H[i];

for(uint t=0; t<64; t++)
{

uint t1, t2;
t1 = work[7] + fn1(work[4]) + ch(work[4], work[5], work[6]) + K[t] + W[t];
t2 = fn0(work[0]) + maj(work[0], work[1], work[2]);
work[7] = work[6];
work[6] = work[5];
work[5] = work[4];
work[4] = work[3] + t1;
work[3] = work[2];
work[2] = work[1];
work[1] = work[0];
work[0] = t1 + t2;

}

for(uint i=0; i<8; i++)
{
H[i] = work[i] + H[i];
}
}


for(uint i=0; i<8; i++)
{
Sha256HashValue = Sha256HashValue + fromDecimal(H[i], 16);
}
return Sha256HashValue;
}

I know virtually nothing about COBOL, let alone COBOL to C++ interoperability. So be forewarned.

These are a couple of things that come to mind:

C++ function names are decorated with type information.
http://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_C.2B.2B
So you might want to use an extern "C" linkage directive.

COBOL strings (AFAIK) are not null-terminated;
so you may have to pass the length of the string as a separate parameter.

Something like:
(the COBOL syntax is more likely to be wrong than correct; just there to convey the general idea)

1
2
3
4
5
6
7
8
9
05 I-PARM PIC X(214).
05 I-PARM-LENGTH USAGE BINARY-LONG-UNSIGNED.
05 SHA256-HASH PIC X(32)

* move message to I-PARM

MOVE 214 TO I-PARM-LENGTH.

CALL "GetSha256Hash" USING I-PARM, I-PARM-LENGTH, BY REFERENCE SHA256-HASH.


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <algorithm>

extern "C"
{
     __declspec(dllexport) void GetSha256Hash( const char* msg, unsigned long msg_size, char result[32] )
     {
         std::string message( msg, msg_size ) ;
         std::string hash ;
         // compute 256-bit SHA256 hash and put it into 'hash'
         std::copy( hash.begin(), hash.end(), result ) ;
     }
}


Assumes that a byte is an octet in both COBOL and C++.



Topic archived. No new replies allowed.