skip to Main Content

I’m extperimenting COBOL last times and I having trouble with link and external call with COBOL

First, i have a simple test lib source code:

// testlib.c
#include <stdio.h>

void CFUNC() {
    printf("This is a C function.n");
}

I compile my lib with theres:
gcc -c -o testlib.o testlib.c and after ar rcs libout.a testlib.o

I have a cobol source file:

IDENTIFICATION DIVISION.
PROGRAM-ID. CallCFunction.


PROCEDURE DIVISION.
    EXTERNAL "CFUNC".
    CALL "CFUNC"
    STOP RUN.

I compile this with cobc -x -free -o cobollink cobollink.cbl -L/path/to/lib -lout

I’m having this error:

cobollink.cbl:6: error: syntax error, unexpected EXTERNAL

The "EXTERNAL" should not be here ? The code compile well without EXTERNAL but CFUNC is not found

How can I call my C function ?

$cobc -V
cobc (GnuCOBOL) 4.0-early-dev.0
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Keisuke Nishida, Roger While, Ron Norman, Simon Sobisch, Edward Hart
Built     Sep 14 2021 19:23:38
Packaged  Jun 06 2020 20:56:36 UTC
C version "11.2.0"

(using UBUNTU 22.04)

Thanks

2

Answers


  1. I was able to get it working via the following:

    • Same CFUNC file contents

    Couple changes:

    • Compiled with gcc -c CFUNC.c -o CFUNC.o (not as static lib, if you need it static I can look into that, just going off of what I found quickly researching)
    • New test.cbl (just remove EXTERNAL keyword):
    IDENTIFICATION DIVISION.
    PROGRAM-ID. CallCFunction.
    
    
    PROCEDURE DIVISION.
        CALL "CFUNC"
        STOP RUN.
    
    
    • Compile that with cobc -x -free -o cobollink test.cbl CFUNC.o (assuming CFUNC.o and COBOL source are in same folder)

    Output:

    <path...>/Experiments/COBOL/c_from_cobol$ ./cobollink 
    This is a C function.
    
    Login or Signup to reply.
  2. You might be interested in trying gcobol, a new COBOL compiler based on gcc. Under development for 18 months, and still going strong.

    $ gcc -c -o test.o  test.c
    $ gcobol -oo test.cbl test.o
    $ ./o
    This is a C function.
    $ 
    

    In gcobol, the CALL target by default is converted to lowercase to make it case-insensitive, consistent with the ISO standard. To make your example work, either the C function must be lowercase, or you use the CDF CALL-CONVENTION statement to invoke the target verbatim.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search