mingwcobolmingw-w64chocolateygnucobol

Resolving MinGW Error with GnuCOBOL on Windows


I am not certain whether this is a MinGW issue, or if there's some problem with the way that GnuCOBOL's cobc is calling the C compiler. I've looked through the manual, but I'm not seeing anything there to suggest I'm doing anything wrong here. Of course that doesn't mean I'm not, but if I am, I am not aware of it.

System Info

I'm working on Windows 11 Pro.

I installed GnuCOBOL through chocolatey. Here is the output from cobc --version:

cobc (GnuCOBOL) 3.2.0
Copyright (C) 2023 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     Jul 28 2023 19:10:09
Packaged  Jul 28 2023 17:02:56 UTC
C version (MinGW) "9.2.0"

I also installed the C compiler from chocolatey, and here is the output from gcc --version:

gcc.exe (x86_64-posix-seh-rev0, Built by MinGW-Builds project) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I am editing in VS Code but I am not working with any extensions, and I am trying to compile from a Powershell session. For all intents and purposes, I may as well be editing in Notepad.

Code

I am trying to compile this simple program:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
DATA DIVISION.
    WORKING-STORAGE SECTION.
        77 X PIC 99.
        77 Y PIC 99.
        77 Z PIC 99.
PROCEDURE DIVISION.
    SET X TO 10.
    SET Y TO 25.
    ADD X Y GIVING Z.
    DISPLAY "X + Y = "Z.
STOP RUN.

Some of you may recognize it from JDoodle as the default example COBOL program, and it compiles and runs fine there; here's the output when run from JDoodle:

X + Y = 35

Problem

However, when I try to run cobc -x hello.cbl in the same directory as hello.cbl, I see:

hello.cbl:1: note: free format detected
    1 > IDENTIFICATION DIVISION.
    2 | PROGRAM-ID. HELLO-WORLD.
    3 | DATA DIVISION.
In file included from C:/ProgramData/mingw64/mingw64/x86_64-w64-mingw32/include/crtdefs.h:10,
                 from C:/ProgramData/mingw64/mingw64/x86_64-w64-mingw32/include/stddef.h:7,
                 from C:/ProgramData/mingw64/mingw64/lib/gcc/x86_64-w64-mingw32/13.2.0/include/stddef.h:1,
                 from C:\ProgramData\chocolatey\lib\gnucobol\tools\include/string.h:53,
                 from C:\Users\[REDACTED]\AppData\Local\Temp\cob20568_0.c:8:
C:/ProgramData/mingw64/mingw64/x86_64-w64-mingw32/include/corecrt.h:35:18: error: expected ';' before 'typedef'
   35 | __MINGW_EXTENSION typedef unsigned __int64 size_t;
      |                  ^~~~~~~~
      |                  ;
C:/ProgramData/mingw64/mingw64/x86_64-w64-mingw32/include/corecrt.h:45:18: error: expected ';' before 'typedef'
   45 | __MINGW_EXTENSION typedef __int64 ssize_t;
      |                  ^~~~~~~~
      |                  ;
C:/ProgramData/mingw64/mingw64/x86_64-w64-mingw32/include/corecrt.h:63:18: error: expected ';' before 'typedef'
   63 | __MINGW_EXTENSION typedef __int64 intptr_t;
      |                  ^~~~~~~~
      |                  ;
C:/ProgramData/mingw64/mingw64/x86_64-w64-mingw32/include/corecrt.h:76:18: error: expected ';' before 'typedef'
   76 | __MINGW_EXTENSION typedef unsigned __int64 uintptr_t;
      |                  ^~~~~~~~
      |                  ;
C:/ProgramData/mingw64/mingw64/x86_64-w64-mingw32/include/corecrt.h:89:18: error: expected ';' before 'typedef'
   89 | __MINGW_EXTENSION typedef __int64 ptrdiff_t;
      |                  ^~~~~~~~
      |                  ;
C:/ProgramData/mingw64/mingw64/x86_64-w64-mingw32/include/corecrt.h:124:18: error: expected ';' before 'typedef'
  124 | __MINGW_EXTENSION typedef __int64 __time64_t;
      |                  ^~~~~~~~
      |                  ;

I did go into corecrt.h and confirm that each instance of __MINGW_EXTENSION typedef... does exist. I tried correcting these six lines to __MINGW_EXTENSION; typedef... but I did not change anything else.

After making that change, I again tried to run cobc -x hello.cbl and this time got:

hello.cbl:1: note: free format detected
    1 > IDENTIFICATION DIVISION.
    2 | PROGRAM-ID. HELLO-WORLD.
    3 | DATA DIVISION.
C:/ProgramData/mingw64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find /LIBPATH:C:\ProgramData\chocolatey\lib\gnucobol\tools\lib\libcob.lib: Invalid argument
collect2.exe: error: ld returned 1 exit status

I am a professional developer but I have absolutely no C programming experience. I am just trying to familiarize myself with COBOL for fun. Clearly, I'm having some C compiler issues, but I cannot find anything online specific to GnuCOBOL or to the specific version of MinGW that I've installed. I am finding loads of general C debugging advice, but I am not sure how to (or if I should) apply that guidance to the MinGW version I have installed. I feel that I have gone off the path entirely when I find myself debugging the files internal to that package.

Has anyone else here seen this issue? If you have, do you have any guidance?

Also, should I not try COBOL before having a go at C?

I plan on just using JDoodle for now, but I would like to get this working locally if possible.

EDIT: Thanks to @simon-sobisch's extremely helpful answer, I was able to resolve the issue, but I did have to take some extra steps.

First, GnuCOBOL does come with its own compiler but chocolatey didn't put it on PATH, apparently. I had to manually add it to PATH. At least of this writing, it's under C:\ProgramData\chocolatey\lib\gnucobol\tools\bin.

Per the linked SourceFourge forum post, I did have to edit COB_LIB_PATH and COB_LIBS, and I had to create (not edit) COB_LIBRARY_PATH, like so:

PS C:\Users\[REDACTED]\repos\cobol> gci env:* | Where-Object {$_.Name -like 'COB_*'}

Name                           Value
----                           -----
COB_LIB_PATH                   -L"C:\ProgramData\chocolatey\lib\gnucobol\tools\lib"
COB_LIBS                       -lcob
COB_CONFIG_DIR                 C:\ProgramData\chocolatey\lib\gnucobol\tools\config
COB_LIBRARY_PATH               /LIBPATH:"C:\ProgramData\chocolatey\lib\gnucobol\tools\extras"
COB_CFLAGS                     -I"C:\ProgramData\chocolatey\lib\gnucobol\tools\include"

And, success!

PS C:\Users\[REDACTED]\repos\cobol> cobc -x hello.cbl
hello.cbl:1: note: free format detected
    1 > IDENTIFICATION DIVISION.
    2 | PROGRAM-ID. HELLO-WORLD.
    3 | DATA DIVISION.
PS C:\Users\[REDACTED]\repos\cobol> .\hello.exe
X + Y = 35

Solution

  • The choco package has GCC included (the version it was tested with), you don't need to "extra" install it. Ensure that its "bin" directory is in path and the GCC found and used (if you don't need the choco GCC otherwise, I'd suggest to uninstall it=)

    In theory the choco package should "just work", but there is at least one issue discussed in the GnuCOBOL discussion forum about linking which notes that you likely need to adjust COB_LIBS and COB_LIB_PATH after installing (because of a bug in the choco package).