Menu

Help calling c program from GNUCobol

A Delosa
2022-06-18
2022-06-19
  • A Delosa

    A Delosa - 2022-06-18

    I do apologise in advance. I only discovered this forum after raising an issue on the project. I am a pretty new c programmer so its possible it is a user issue, but I am really stuck and looking for advice.

    See [bugs:#833]

    In summary, trying to use libffi. Setting a reference to a libffi value stops the cobol program from finding the entry point of the c module.

    say.c

    #include <stdio.h>
    #include <ffi.h>
    
    int say(char *hello, char *world)
    {
        int num_dgs = 1;
        ffi_type *args[num_dgs];  
        args[0] = &ffi_type_uint32;  //  This line causes issue. If commented out, it finds the entry point.
        printf("Got to end\n");
        return(0);
    }
    

    hello.cob

           IDENTIFICATION DIVISION.
           PROGRAM-ID. hello.
           ENVIRONMENT DIVISION.
           DATA DIVISION.
           WORKING-STORAGE SECTION.
           01 hello PIC X(7) VALUE "Hello, ".
           01 world PIC X(6) VALUE "world!".
           PROCEDURE DIVISION.
           CALL "say" USING hello world.
           STOP RUN.
    

    compile and run

    cobc -x -o hello hello.cob
    gcc -fPIC -shared -o say.so say.c
    ./hello
    

    Receive error libcob: error: entry point 'say' not found
    I have the COB_LIBRARY_PATH set to .

    The strange thing is, this works fine on MacOS where I do much of my dev. It seems to be an issue on Linux.
    The difference there is it used dylib instead of so files, but otherwise the same.

    Anyhow, any advice or help would be much appreciated.
    Thanks!
    Anthony

     

    Related

    Bugs: #833


    Last edit: Simon Sobisch 2022-06-18
  • serge lacombe

    serge lacombe - 2022-06-18

    Hi.

    have you tried : cobc -x -o hello.cob say.c -lffi

    Hope it helps.

     
  • Simon Sobisch

    Simon Sobisch - 2022-06-18

    If you comment the libffi call out then the module can be loaded, if you don't then you need it, otherwise it isn't possible to load the module.

    Note: you don't need to use your native C compiler if you want to create C modules that are used for COBOL the easiest option is to use cobc for that.

    This leads to either:

    cobc -x -o hello hello.cob
    cobc -m -o say.so say.c -lffi
    

    or, because "output file from input file default"

    cobc -x hello.cob
    cobc -m say.c -lffi
    

    or one fat binary as serge noted:

    cobc -lffi -x hello.cob say.c
    

    Note: depending on your linker defaults this may not work and need to either use an additional -Q '-Wl, --no-as-needed' option or a change to CALL STATIC 'say'.

    In any case it is a good thing to explicit define "external" entry points as those - in your case extern int say.

     
  • A Delosa

    A Delosa - 2022-06-18

    THANKS!.. That was simple. I am not sure why on MacOS it behaves differently but I guess that's just the way things are. Thanks again!

     
    • Simon Sobisch

      Simon Sobisch - 2022-06-19

      The outline above works on every system, including MacOS and Win32.

       

Anonymous
Anonymous

Add attachments
Cancel