- You can use any text editor to write your code. When you save your program, it must have the correct extension of .cbl. You can also edit programs using a text editor. As you can see here, I'm using Notepad++, which is a free, open source editor available at the notepad-plus-plus.org website. It helps if you choose an editor that can support your COBOL syntax. For example, in Notepad++, if I go to Language and go down to C, you'll notice that under C I can choose C, C#, C++, and COBOL.
I have COBOL chosen, and that's why this particular program has the highlighting that it does. It helps with the code editing. The execution of every COBOL program includes three steps. First, the source code, which is what you see here, must be run through a compiler. Once it compiles successfully, then it runs through a linker, and, finally, a load module. For this, I'm using GnuCOBOL, which I already downloaded and installed all the packages that I need.
It is helpful to create a folder to organize your COBOL programs. I already created a folder called PGMS in my OC directory on my C: drive. To compile using GnuCOBOL, you use the cobc command and the file name, including the extension. Let's go ahead and compile this sample program and then run it. First, I'll launch my Cygwin terminal window. I have an icon here on my taskbar. When the window first opens, it takes you right to your Cygwin folder.
So I need to navigate to the place on my computer where I have my program saved. So I'm going to use the command cd, which is change directory, space. And I want to go to the C: drive. I'll do C:. Now notice in the Cygwin, you'll use the forward slash where you would normally use a backslash in a Windows command prompt. And I'm going to type in oc for open COBOL, that's a folder I created, and finally pgms, which is where I have my COBOL programs.
Let's type dir to see what's in this folder. As you can see, I have two programs and two executables because I've already compiled both the sample and the coding rules programs before. But let's compile it again. So I type in cobc. That's the COBOL compile executable, space, -x , space, and then the file name, including the extension. So I'm going to do 01_05_sample.cbl and I'm going to hit enter.
It came back with no errors, so now I can actually run the program. To run it, I do ./ and the name of the program, this time with no extension. And my program asks for my name and it says Hello Peggy. So at this point I ran the compiler, which compiles and links in one step. And then I ran the executable. Did you notice that I used the option -x when I did the compile? There are many different options you can use in the GnuCOBOL.
The option that I used, -x, is used when you are ready to build an executable. So I've already worked through any compile time errors and I wanted to create my executable. That's why I used the -x. Some other options are -m, which would build a dynamically loadable module, which is the default, - debug, which might come in handy, which enables all runtime error checking. and -o, with the file name. You can use that if you want to specify a place for the output to go.
There are many more options available, so check the GnuCOBOL programmers guide for a complete list. The compiler takes the original source code and converts it from text to machine language or object code. The next step takes the newly compiled object code and combines or links it with previously written subroutines and other object modules to produce a load module. The last step is the execution of the compiled and linked module. This step may have additional input and it might produce output reports or files.
The linker step is similar to using a calculator. When you want to find the square root of a number you just enter the number in the calculator and hit the square root botton. The calculator has a subroutine that someone else has written and tested. The subroutine knows how to find the answer. You don't need to enter a complicated math formula for finding the square root. The subroutine that runs on the calculator is linked. When you press the button, it returns the result. Okay, so that's how you can compile and execute or run your programs.
I do want to note that every operating system has its own specific commands, but the same three steps are required. Compile, link, and execute. Therefore it is necessary to learn the commands of your particular configuration in order to run the compiler, the linker, and to execute your COBOL program. The Cygwin terminal commands that might be helpful to know are the cd, which, again, is change directory, the cd space and then the new directory that you want to go to, so cd C: will take me to the C directory.
Cls is a nice command to know, which clears the screen. Dir gives me a list of everything in the current directory, so, in my case, since I'm on the C: drive it shows me everything on my C: drive. Finally, by typing in the word help you can get a list of all the commands available to us in this Cygwin terminal window.
This course is designed to help new and experienced programmers alike add COBOL (or add COBOL back) to their skill set. Peggy Fisher shows how to get a COBOL development environment up and running and how to start programming. She reviews COBOL's data types and constants, control structures, file storage and processing methods, tables, and strings. Challenges issued along the way will help you practice what you've learned.
- Downloading and installing Cygwin and GNU COBOL
- Editing, compiling, linking, and running COBOL programs
- Describing data in COBOL
- Working with verbs and expressions
- Using branching
- Reading and writing sequential files
- Updating and deleting records
- Working with relative and indexed files
- Creating and searching tables
- Handling strings