Sublime Forum

Cobol compiler help

#1

I currently have cobol installed and I am just trying to make sure everything compiles correctly. However I keep getting an error whenever I compile a program. I have my syntax and my build system set to OpenCobol.

the error I get:
/bin/sh: cobc: command not found
[Finished in 0.0s with exit code 127]

Here is my code:
Identification Division.
PROGRAM-ID. hello_world.

Procedure Division.
DISPLAY ‘Hello world’.
STOP RUN.

Anything would really help me right now.

Thank you.

0 Likes

#2

The error message is telling you that cobc isn’t anywhere in the path, so the shell can’t find it to run it.

To fix this, you need to either add the path that cobc is installed in to your user path, or install the cobc package such that it ends up in a standard location (e.g. /usr/bin or /usr/local/bin).

0 Likes

#3

okay. I understand what you mean. I have been trying to do that but it is really weird to do on my Mac. Or maybe I’m making it more difficult than it actually is. How would I do that?

0 Likes

#4

Mac’s are a bit tricky because when MacOS runs a GUI program it gets a different path than what applications launched from the terminal get. Sublime runs programs using the path that it inherited when it got started, so the path that it sees is different from what you might expect.

How you go about it seems to vary based on what version of MacOS you have, so I would recommend a Google search for the specific version that you have to get instructions on how to modify it for your install.

With that said, there’s another way around this: override the build system that’s being used or duplicate it with a new name and use that one instead. These have the advantage of working no matter what version of MacOS you have.

The disadvantage (for the override method) is that if the package ever updates the build system, your modification will override the new version as well and you won’t see the changes. That’s not a show stopper though, just something to be aware of.

For either method, the first thing you need to do is take a look at what the current build system is doing. I don’t have a package for this installed, so this is based on the first hit in google for such a package being this page. The steps are still the same, although the actual file names and content may differ.

  1. Install PackageResourceViewer, if you haven’t already; it’s a great tool for peeking inside of installed packages and making modifications.
  2. Open up the command palette (Tools > Command Palette or Shift+⌘+P on MacOS)
  3. Filter down the list of commands to Package Resource Viewer: Open Resource (entering prv is usually enough to get close) and run it
  4. The list of installed packages opens; filter it by entering Cobol and select the package that pops up
  5. The list of all of the files that exist in this package will be presented, so select OpenCOBOL.sublime-build (you can enter the text build to filter to just build files) and hit enter to open the file, then close the command palette if it’s still open.

You should now be looking at the contents of the sublime build file that you’re using. The build file from the link I mentioned above looks like this; yours may be different if you’re not using the same package.

{
  "selector": "source.cobol",
  "cmd": [
    "cobc -fsyntax-only $file"
  ],
  "working_dir": "$file_path",
  "shell": true,
  "file_regex": "^([^:]+):([0-9]+):",
  "osx": {
    "env": {
      "PATH": "/usr/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:~/bin:$PATH"
    }
  },
  "linux": {
    "env": {
      "PATH": "/usr/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:~/bin:$PATH"
    }
  }
}

From here, you can modify the PATH setting under osx to include the path to your cobc compiler (path settings are separated from each other by the colon character) if your build system is similar, add such a section if your build system doesn’t have one, or modify the cmd portion to specify the absolute path to the command so that it doesn’t need to be looked up in the path at all.

If you modify the file and hit save, the modification will be saved not in the actual package, but in your own Packages directory as an override. This keeps the installed package pristine and gives you your change as if it was a part of the package, but it does mean that if the package itself updates this file, sublime will still use yours.

Alternatively, you could save it in your Packages/User folder with a name like CustomCobol.sublime-build or something similar, and then select that build system from the menu so that yours gets used instead.

2 Likes

#5

thank you so much, it took some doing but I was able to finally get it working.

2 Likes

#6

That was extremely helpful. But, I’m having trouble finding the path to my CoBOL compiler. It’s freshly installed on my mac as of an hour ago, and I have no clue how to go about doing this…

0 Likes

#7

If the installer for the compiler and its tools doesn’t tell you where it installed, then the only way I know of to be able to figure out where it is (assuming it’s not in one of the default locations, in which case it would “just work”) would be to search your Mac to try to find it.

0 Likes

#8

I got that far at this point (haha), but when I try to build I get this error:

cobc: /Users/max/Documents/School/Intro: No such file or directory
[Finished in 0.0s with exit code 1]
[cmd: [‘cobc -fsyntax-only /Users/max/Documents/School/Intro to Biz Systems/Programs/Class9.cob’]]
[dir: /Users/max/Documents/School/Intro to Biz Systems/Programs]
[path: /usr/local/Cellar/open-cobol:/usr/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:~/bin:$PATH]

After removing spaces in the directory of the “Intro to (…)” file and building again, nothing happens and the console is blank.

After trying with another program I put in my home directory, I get this error:

/Users/max/Desktop/cobolTEST.txt:1: Error: Invalid indicator ‘F’ at column 7

This, I guess, is a separate issue with comments in sublime but nevertheless I’m super confused. Any ideas?

Funny thing is, it all works perfectly through my terminal!

0 Likes

#9

You’ve rightly determined that the reason the first build failed was because of spaces in the filename. As a general rule of thumb, any time there is a file name or file path variable, you should wrap it in double quotes so that the underlying code knows that all of the space separated components should be taken together…

Since sublime-build files are JSON and double quotes are special in JSON, you need to precede each with a \ character to quote them:

    "shell_cmd": "cobc \"$file\"",

If your build file is the one I mentioned above, the problem is probably because it specified -fsyntax-only. According to the manual for GnuCOBOL (which used to be OpenCOBOL, apparently):

-fsyntax-only
    Check syntax only; don't emit any output. 

As such I would guess the first build outputs nothing because the syntax is correct, but the second build generates output because of the syntax error. If that’s the case, modifying the build by removing that part of the command line will probably do what you want.

Possibly you want a version of the build that just checks syntax without actually running the program, in which case you’d use a variant instead. An example of that would look like this:

{
    "shell_cmd": "cobc \"$file\"",
    "file_regex": "^([^:]+):([0-9]+):",
    "selector": "source.cobol",
    "working_dir": "$file_path",

    "osx": {
        "env": {
            "PATH": "/usr/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:~/bin:$PATH"
        }
    },
    "linux": {
        "env": {
            "PATH": "/usr/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:~/bin:$PATH"
        }
    },

    "variants":
    [
        {
            "name": "Syntax Check",
            "shell_cmd": "cobc -fsyntax-only \"${file}\"",
        }
    ]
}

In a variant, the build information is taken from the main build and anything inside of the variant entry overrides the same key in the overall build. So here it just specifies a different command to execute but everything else (path, etc) is still taken from the main build.

0 Likes

#10

I just realized I forgot this part of your question; that error message is being generated by the COBOL compiler, not by Sublime, so it’s not really Sublime related except that Sublime is showing you the error message.

With the proviso that the last time I used COBOL was in College roughly a billion years ago, where we coded on an IBM 3270 mainframe, my recollection is that comments in COBOL were signified by a / or * in column 7, not an F.

If I am recalling correctly, a / in that column would make sure that when the line printer spat out your program listing that the line was always at the start of a fresh page on the printout, while the * was just a regular comment. So you would for example document your procedure as / then * to make it be on its own page while wishing (in my case anyway) that you were entering those characters into a C program instead of COBOL.

Assuming that is part of some standard and not just what IBM did in their COBOL compiler (and that I’m not misrembering, which is fairly likely), that might be why you’re getting that error in this case from the compiler that you’re using.

0 Likes

#11

Yup, you were completely right about -fsyntax.

Using -free stopped the “Invalid indicator” error, but still no output being displayed. Also, I found no slashes in the syntax, apart from some *> comments, which I don’t think are the issue.

The -j option (“Run program after build”) isn’t recognized by the compiler (probably because I’m using openCobol not GNUCobol ). But, -x, which creates an executable of the program in that directory, is a pretty good alternative.

Maybe there’s another option that I’m supposed to be using?

0 Likes

#12

Unfortunately that post tapped most of my memory for COBOL in general; If there’s an option to create an executable there might be an option to run it. If not, you can modify the build to first compile and then run the executable that’s generated.

What name is the executable generated as? Does it follow the filename of the input?

0 Likes

#13

The file saved as a regular executable.

I actually had to do exactly that by modifying the build to ./ the executable (which has the same filename as the compiled file, ie. File.cob, minus the .cob extension).

In my case, piping the filename and cutting would look like this:

“cobc -free -x $file;./echo /Users/max/Documents/School/67-211/Programs/Class7.cob | cut -d / -f 8 | cut -d . -f 1

Once you build, all the terminal output is printed in Sublime, so I guess it works for the most part!

0 Likes

#14

Glad you got things working! :thumbsup:

As a tip, the same way that $file expands to the name of the file you’re editing, $file_path gives you the path that the file is stored in and $file_base_name gives you the name of the file without an extension.

So a version of the command you outlined above that’s not dependent on the name of the file you’re editing would look like this:

"shell_cmd": "cobc -free -x \"${file}\" && \"${file_path}/${file_base_name}\"",

Here the && will make the shell only execute the program if the compiler successfully compiled it.

1 Like