Search code examples
compiler-errorssegmentation-faultcobollibcgnucobol

cobc generated "Segmentation fault" for gnuCOBOL


I'm trying to compile and execute the hello world example in: https://gnucobol.sourceforge.io/historical/open-cobol/Hello-World-.html

But when I compile with:

    $ cobc hello.cob

I get a hello.so file. I execute with ./hello.so and get this error.

Segmentation fault.

This program not uses memory.

I'd added the libcob.so to LD_LIBRARY_PATH and ldconfig.

When use:

    $ cobc -x -free hello.cob

It works but without add comments in .cob file.

Also is very rare that cobc generates an .so file and not an .a file.


Solution

  • cobc hello.cob is identical to cobc -m hello.cob, from cobc --help:

      -x         build an executable program
      -m         build a dynamically loadable module (default)
    

    A ".so" file is a shared library, so nothing you commonly want to execute, because most shared libraries don't have a "main" symbol entry point.

    ... but you've followed some docs, I guess those are heavily outdated and may were working that way around 2002 /´(mind the URL "historical/open-cobol"). You likely want to have a look at the manual, if you don't want to execute "info gnucobol" you may want to have a look at its simplest all-in-one-HTML form: https://gnucobol.sourceforge.io/doc/gnucobol.html and in any case you likely want to check out the GnuCOBOL Programmer's Guide

    If you want to execute hello.so use cobcrun hello (GnuCOBOL's module loader).

    Comments don't work with cobc -x -free hello.cob as the file is a fixed-form reference-format (in which line-comments start with * in the indicator column = 7) and you told cobc to handle it as free-form reference-format (which has no indicator column at all and where comments start in any position with *>).

    From cobc --help:

      -F, -free   use free source format
      -fixed      use fixed source format (default)
    

    Also is very rare that cobc generates an .so file and not an .a file.

    Yes, that's rare on GNU/Linux, but you can have it your way: cobc -x -o hello.a hello.cob