clinkercobol

call external C library from COBOL


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


Solution

  • I was able to get it working via the following:

    Couple changes:

    IDENTIFICATION DIVISION.
    PROGRAM-ID. CallCFunction.
    
    
    PROCEDURE DIVISION.
        CALL "CFUNC"
        STOP RUN.
    
    

    Output:

    <path...>/Experiments/COBOL/c_from_cobol$ ./cobollink 
    This is a C function.