2

I'm trying to practice a C program (hello.c) calling cobol program (say.cob) from the manual gnucobol.

    ---- say.cob ------
    IDENTIFICATION DIVISION.
    PROGRAM-ID. say.
    ENVIRONMENT DIVISION.
    DATA DIVISION.
    LINKAGE SECTION.
    01 HELLO PIC X(6).
    01 WORLD PIC X(6).
    PROCEDURE DIVISION USING HELLO WORLD.
    DISPLAY HELLO WORLD.
    EXIT PROGRAM.

    ---- hello.c -
    #include <libcob.h>
    extern int say(char *hello, char *world);
    int
    main()
    {
     int ret;
     char hello[7] = "Hello ";
     char world[7] = "World!";
     cob_init(0, NULL);
     ret = say(hello, world);
     return ret;
     }


     C:\Users\S M Rao>gcc -c ‘cob-config --cflags‘ hello.c
     gcc: error: `cob-config: No such file or directory
     gcc: error: unrecognized command line option '--cflags`'

    if I run with commands 

    cobc -c hello.c 
    cobc -c -static say.cob 
    cobc -x -o hello hello.o say.o

   getting following error 

    C:\Users\S M Rao>cobc -x -o hello hello.o say.o
    hello.o:hello.c:(.text+0x5c): undefined reference to `say'
    collect2.exe: error: ld returned 1 exit status

I can see cob-config is present in gnucobol folder. And in environment variables COB_CONFIG_DIR %COB_MAIN_DIR%\config

what could be the problem? any help please?

bin folder

env variables

env variables

SRI
  • 131
  • 8

1 Answers1

2

cob-config would need to be an executable script, which it commonly is. As you specify windows paths I assume you use that - and this one cannot run shell scripts. You may get around that with and additional cob-config.bat that executes this shell script, but In this case it will output mingw/wsl/cygwin/... paths that likely cannot be used in the Windows gcc.

Solutions:

  1. run gcc in the matching shell, not from the windows side and have cob-config as an executable script in $PATH
  2. use cobc's feature to call the C processor (and if wanted also the linker) for you: cobc -c hello.c
Simon Sobisch
  • 6,263
  • 1
  • 18
  • 38
  • Hi thanks for your reply. I made cob-config as executable but still getting the error. Modified question with screenshots. Can you please elaborate your answer? Thanks – SRI Apr 16 '21 at 08:35
  • The issue is that you try to execute a posix shell script inline with `inlined` posix command in a Windows environment without a posix shell. This simply won't work. To make it work you need execute that command in a shell, not in cmd, and then likely get to issues of not matching folders. As noted in the answer: just let `cobc` execute any necessary C compile, also for non-COOBL parts and it will do the right thing + link to libcob. – Simon Sobisch Apr 17 '21 at 07:47
  • Thanks for reply. I installed cygwin and ran above commands, but getting same error – SRI Apr 17 '21 at 11:19
  • So you do run the commands within the cygwin shell (note that is commonly an overkill if it is just done for any single program and the installed tools are best only mixed with other cygwin installed tools) and you do have `cob-config` in cygwin's `$PATH`? What about letting cobc handle all that in drop the "use the output of an inlined script" approach? – Simon Sobisch Apr 18 '21 at 10:23
  • I'm not getting your words.. I have downloaded Gnucobol using windows 10. Can you please let me know what else needs to be downloaded in order to run above programs? – SRI Apr 19 '21 at 10:06
  • In order to run above programs on Win10: don't call `gcc`directly, but just call `cobc` instead, this way there's no need for using `cob-config` at all. – Simon Sobisch Apr 20 '21 at 10:09
  • Thanks for reply. Tried with cobc but getting reference error like this C:\Users\S M Rao>cobc -c hello.c C:\Users\S M Rao>cobc -c -static say.cob C:\Users\S M Rao>cobc -x -o hello hello.o say.o hello.o:hello.c:(.text+0x5c): undefined reference to `say' collect2.exe: error: ld returned 1 exit status – SRI Apr 21 '21 at 06:38
  • Does say.cob contain a `PROGRAM-ID. 'say' `? Does the order of the object files matter? – Simon Sobisch Apr 21 '21 at 06:49
  • Thanks for reply. Error is program id say is in capitals. So changed to small case and executed successfully. Thank you Simon – SRI Apr 21 '21 at 07:08
  • @SRI You may want to mark this issue as answer - or write your own if it did not solve the issue. And for GnuCOBOL with integrated IDE in 2023 you likely want to switch to the maintained [Gix-IDE](https://github.com/mridoni/gix/blob/main/doc/tutorial.md). – Simon Sobisch Feb 04 '23 at 22:29