Cobol Friends,
Below is a simple C program that calls a Cobol routine. It gives a core dump. The same Cobol compilation works fine when called from Cobol. I've included the source, the compile/execute commands, and some diagnostic data below.
Any ideas on how to call Cobol from C?
thanks,
Peter
+ cat my_cobfunc.cbl
PROGRAM-ID. my_cobfunc.
PROCEDURE DIVISION.
display "Cobol aspect running.".
EXIT.
+ cobc -free -c my_cobfunc.cbl
+ cat main.c
extern void my_cobfunc();
main(){
printf("C main starts.\n");
my_cobfunc();
}
+ gcc -m64 -lcob my_cobfunc.o main.c
+ a.out
C main starts.
n[6]: 6131 Segmentation Fault(coredump)
+ adb
+ 0<<
$c
core file = core -- program ``a.out'' on platform i86pc
SIGSEGV: Segmentation Fault
libcob.so.4.0.0`cob_exit_screen+0xb()
libcob.so.4.0.0`cob_runtime_error+0x95()
libcob.so.4.0.0`cob_fatal_error+0x77()
libcob.so.4.0.0`cob_module_enter+0x3e()
my_cobfunc_+0x2c()
my_cobfunc+0xe()
main+0x1d()
_start+0x6c()
+ cat m.cbl
PROGRAM-ID. m.
PROCEDURE DIVISION.
call "my_cobfunc".
+ cobc -free -x m.cbl my_cobfunc.o
+ ./m
Cobol aspect running.
+ cobc -free -C my_cobfunc.cbl
+ head -10000 my_cobfunc.c my_cobfunc.c.h my_cobfunc.c.l.h
==> my_cobfunc.c <==
/* Generated by cobc 2.0.0 */
/*
Generated from my_cobfunc.cbl */
/* Generated at Nov 04 2013 17:10:56 */
/* OpenCOBOL build date Sep 29 2013 11:55:29 */
/* OpenCOBOL package date Feb 11 2012 12:36:31 UTC */
/* Compile command cobc -free -C my_cobfunc.cbl */
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <math.h>
#define COB_KEYWORD_INLINE __inline
#include <libcob.h>
#define COB_SOURCE_FILE "my_cobfunc.cbl"
#define COB_PACKAGE_VERSION "2.0"
#define
COB_PATCH_LEVEL 0
#define COB_MODULE_FORMATTED_DATE "Nov 04 2013 17:10:56"
#define COB_MODULE_DATE 20131104
#define COB_MODULE_TIME 171056
/* Global variables */
#include "my_cobfunc.c.h"
/* Function prototypes */
int my_cobfunc (void);
static int my_cobfunc_ (const int);
/* Functions */
/* PROGRAM-ID 'my_cobfunc' */
/* ENTRY 'my_cobfunc' */
int
my_cobfunc
()
{
return my_cobfunc_ (0);
}
static int
my_cobfunc_ (const int entry)
{
/* Program local variables */
#include "my_cobfunc.c.l.h"
/* Start of function code */
/* CANCEL callback */
if (unlikely(entry < 0)) {
goto P_cancel;
}
/* Check initialized, check module allocated, */
/* set global pointer, */
/* push module stack, save call parameter count */
cob_module_enter (&module, &cob_glob_ptr, 0);
/* Set address of module parameter list */
module->cob_procedure_params = cob_procedure_params;
/* Set frame stack pointer */
frame_ptr = frame_stack;
frame_ptr->perform_through = 0;
/* Initialize program */
if (unlikely(initialized == 0))
{
goto P_initialize;
}
P_ret_initialize:
/* Increment module active */
module->module_active++;
/* Save number of call params */
module->module_num_params = cob_glob_ptr->cob_call_params;
/* Entry dispatch */
goto l_2;
/* PROCEDURE DIVISION */
/* Line: 3 : Entry my_cobfunc : my_cobfunc.cbl */
l_2:;
/* Line: 3 : DISPLAY : my_cobfunc.cbl */
cob_display (0, 1, 1, &c_1);
/* Line: 4 :
EXIT : my_cobfunc.cbl */
/* Program exit */
/* Decrement module active count */
if (module->module_active) {
module->module_active--;
}
/* Pop module stack */
cob_module_leave (module);
/* Program return */
return b_1;
/* Program initialization */
P_initialize:
cob_check_version (COB_SOURCE_FILE, COB_PACKAGE_VERSION, COB_PATCH_LEVEL);
/* Initialize module structure */
module->module_name = "my_cobfunc";
module->module_formatted_date = COB_MODULE_FORMATTED_DATE;
module->module_source = COB_SOURCE_FILE;
module->module_entry.funcptr = (void *(*)())my_cobfunc;
module->module_cancel.funcptr =
(void *(*)())my_cobfunc_;
module->collating_sequence = NULL;
module->crt_status = NULL;
module->cursor_pos = NULL;
module->module_ref_count = NULL;
module->module_path = &cob_module_path;
module->module_active = 0;
module->module_date = COB_MODULE_DATE;
module->module_time = COB_MODULE_TIME;
module->module_type = 0;
module->module_param_cnt = 0;
module->module_returning = 0;
module->ebcdic_sign = 0;
module->decimal_point = '.';
module->currency_symbol = '$';
module->numeric_separator = ',';
module->flag_filename_mapping = 1;
module->flag_binary_truncate = 1;
module->flag_pretty_display = 1;
module->flag_host_sign = 0;
module->flag_no_phys_canc = 0;
module->flag_main = 0;
module->flag_fold_call = 0;
module->flag_exit_program = 0;
/* Initialize cancel callback */
cob_set_cancel (module);
/* Initialize WORKING-STORAGE */
b_1 = 0;
initialized = 1;
goto P_ret_initialize;
/* CANCEL callback handling */
P_cancel:
if (!initialized) {
return 0;
}
if (module->module_active) {
cob_fatal_error (COB_FERROR_CANCEL);
}
b_1 = 0;
cob_cache_free (module);
module = NULL;
initialized = 0;
return 0;
}
/* End PROGRAM-ID 'my_cobfunc' */
/* End functions */
==> my_cobfunc.c.h <==
/* Generated by cobc
2.0.0 */
/* Generated from my_cobfunc.cbl */
/* Generated at Nov 04 2013 17:10:56 */
/* OpenCOBOL build date Sep 29 2013 11:55:29 */
/* OpenCOBOL package date Feb 11 2012 12:36:31 UTC */
/* Compile command cobc -free -C my_cobfunc.cbl */
/* Module path */
static const char *cob_module_path = NULL;
/* Attributes */
static const cob_field_attr a_1 = {0x21, 0, 0, 0x0000, NULL};
/* Constants */
static const cob_field c_1 = {21, (cob_u8_ptr)"Cobol aspect running.", &a_1};
==> my_cobfunc.c.l.h <==
/*
Generated by cobc 2.0.0 */
/* Generated from my_cobfunc.cbl */
/* Generated at Nov 04 2013 17:10:56 */
/* OpenCOBOL build date Sep 29 2013 11:55:29 */
/* OpenCOBOL package date Feb 11 2012 12:36:31 UTC */
/* Compile command cobc -free -C my_cobfunc.cbl */
/* Program local variables for 'my_cobfunc' */
/* Module initialization indicator */
static unsigned int initialized = 0;
/* Module structure pointer */
static cob_module *module = NULL;
/* Global variable pointer */
cob_global
*cob_glob_ptr;
/* Call parameters */
cob_field *cob_procedure_params[1];
/* Perform frame stack */
struct cob_frame *frame_ptr;
struct cob_frame frame_stack[255];
/* Data storage */
static int b_1; /* RETURN-CODE */
/* End of data storage */