Compiling a Program

Index of Topics


Explanation of Compiling

C++, COBOL, Pascal, BASIC, and other programming languages are English-like; a computer is an electro-magnetic machine that doesn't directly understand either English or English-like programming languages. In order for a computer to "execute" or "run" a program, the program must be translated into the electro-magnetic switches that the computer "understands." There are several ways of doing this, including assembling, interpreting, and compiling.

Assembly languages are formed by one English-like code for every instruction of the machine's native language, "machine code." Translation of a program written in an assembly language into machine code is called "assembling" the program. Because assembly language instructions map (via assembling) directly to machine instructions, the resulting machine code is likely to be extremely efficient -- runs fast and conserves memory. These are the primary advantages of assembly- language programming. Disadvantages: it tends to be hard to work with, and, since every model of computer has its own version of assembly language, programs aren't "portable" -- they must be rewritten in order to run on other models of computers. For example, a program written in IBM PC assembly language should run on all IBM PC-compatible computers, but not on Macintoshes.

"High-level languages" such as C++, COBOL, etc., tend to be easier for programmers to work with than assembly languages since an instruction in a high-level language typically is the equivalent of several assembly or machine language instructions. High-level language programs are generally either interpreted or compiled into machine code.

Many beginning students of college-level computer programming know BASIC (not Visual Basic), which is usually interpreted. When an interpreted program runs and control within the program reaches a given instruction (such as a LET statement), the interpreter translates the instruction into machine code and executes the corresponding machine instructions, then goes on to the next instruction of the program WITHOUT REMEMBERING the machine code it has just finished executing. Thus, if an instruction is repeated, it must be re-translated to machine code before it can be re-executed.

By contrast, COBOL and C++ are usually compiled. The compiler translates the program IN ITS ENTIRETY to machine code, and "remembers" all the machine code by saving it in an "executable" file, before the program is run. Thus, when the program is run, translation of source code instructions to machine code has been done in advance; repetition of a source code instruction does not require re-translation of the source code instruction to machine code; hence the program tends to run faster. On some computers where a language can be both compiled and interpreted, the compiled version will run enormously faster -- it's not unusual for the parts of the program not requiring input or output to require 1000 times as much time in the interpreted as in the compiled version.

OK, this clearly indicates that an important advantage of compiling as opposed to interpreting is speed of execution. What could a disadvantage of compiling be? Well, remember we said above that one high-level language instruction is typically equivalent to several machine code instructions. Suppose you are running a program in, say, BASIC, that has 500 BASIC instructions, the average of which is equivalent to 5 machine code instructions and the largest of which is equivalent to 10 machine code instructions. If compiled, this would mean you would need enough memory to hold 5*500 = 2500 machine code instructions in order to run the program; if interpreted, you need enough memory to hold 500 BASIC instructions plus at most 10 machine code instructions (for whatever BASIC instruction is currently executing). Thus, interpreting uses less memory. This was an extremely important factor for the early microcomputers -- machines with small memories -- and probably explains why they typically came equipped with interpreted BASIC.

A compiler typically scans your source code file for "syntax errors," i.e., errors in the use of the programming language's rules concerning formation of statements in the program. Frequently, one actual error will generate several error messages, as the first error may "confuse" the poor compiler. Hence, the first error message is significant; others may or may not be.

If the compiler does not find "serious" syntax errors in your program, it will also typically (the details differ from one compiler to another) produce a file that is either the "executable" version of your program or an almost-executable (it may be an "object" file that must be "linked" to produce an executable file). The executable file is the machine code translation of your source code that the computer uses to execute your program when you give the command to do so. Students should note that the goal of the process is not merely to have your program run. It should run correctly and well. It may be necessary for you to observe your program's behavior, edit its source code, compile again, and run again, many times, before you are satisfied with your program's performance.

On the other hand, if the compiler finds errors in your program, it is necessary for you to edit these errors out of your program (in the editor). You must then re-compile. The cycle of editing and compiling may have to be repeated several times before your program is ready to run. Notice also that you're probably not "done" when your program runs the first time - successful compiling does not guarantee either correct logic (necessary to get correct "answers") or nice input/output. It's the programmer's responsibility to check these matters.

To page index
To Boxer's home page


Compiling Borland C++

Once the Borland C++ software is set up on the computer you use, it seems much easier to use this software than Visual C++ (see below for comparison).  In the following, I assume you are using the student disk that came with your textbook.  This version of the software runs as a console application - that is, in a DOS window.

  1. I recommend that every program have its own folder/directory on your disk.  This is because a program typically has several files associated with it, some created by you (source code files, data files), some created by the compiler, perhaps some (output files) created by running the program.
  2. Editing your files: You may edit your program files and input data files using any text editor, such as Notepad, Wordpad, or the DOS Edit program you may have used to set up the Borland software.  The file with your "main" function should be saved using the extension .cpp - it is customary that other source code files use the extension .h - so, for example, if your program simulates a cash register, your program's folder might be named cash-reg, and the "driver" file (the file containing "main") might be named cashdriv.cpp, and a file containing code for a cash register object might be named reg-obj.h.
  3. Compiling your program: When you have finished editing the files needed by your program, you can compile your program as follows:
  4. Running your program: If there are no error messages, the compiler creates an executable file with the same root name as the driver file and the extension .exe - for example, cashdriv.exe. You can now run your program by entering into the console box a command of the form driverfilename (without any extension) - for example, cashdriv

To page index
To Boxer's home page


Using/Compiling Visual C++

The Visual C++ software on the CD that came with our textbook doesn't seem to be accompanied by documentation (online or otherwise) that explains how one prepares a program for usage. Here's an outline of some steps that prove useful for the program used as Project 0 of CIS 265 during the Fall, 2000, semester. Thanks to David Basamania for showing me some of the following.

Some of the steps described below can be omitted if your program has sufficiently simple structure (e.g., only one source code file, no input file, no output file). However, if you use more than one source code file, or an input file, or an output file, what follows may be the best way to get your program working.

Assembling the files you use - creating a project

It seems a complex (in the sense described above) program must be organized into a "project." This is done as follows:

  1. Click File, New; on the Projects tab, choose Win32 Console Application; choose a project name and disk location (disk ID and folder). This creates a "project" folder whose name is the project name you have chosen. Make sure the Create Workspace option is selected. Click OK and choose Empty Project on the next dialoguebox, and Finish this step (of creating a project). For example, I chose the name "cashreg" since the program simulates a cash register.
  2. In the left frame, bring forward the FileView tab. You should see a small tree structure labeled in the form projectname files -- in our example, cashreg files -- open the tree if necessary, by clicking on the "+" symbol, to reveal folders labeled Source Files, Header Files, and Resource Files. For the current project, the Resource Files folder need not be used.
  3. The Source Files folder is meant to contain the source code for your main (driver) program, which should have the extension .cpp Right click the Source Files folder. From the resulting menu, choose Add Files to Folder, and search your disk to retrieve the desired source code file(s). This folder is NOT meant to contain "header" files -- those with extension .h -- even if these are "source code" files in the sense of being files you have created. For the current example, I only put the file containing my main function -- I call this file driver.cpp -- into this folder.
  4. Similarly, right click on the Header Files folder and retrieve all header files your program uses. Header files are generally files mentioned in #include directives. These include both source code files you have created with extension .h (in the current example, bill-obj.h) and library files, which you typically retrieve from the hard drive's section in which the C++ software is installed -- on my machine, that's C:\Program Files\Microsoft Visual Studio\Vc98\Include from which I select those needed (in the initial project, these are Iostream.h, Fstream.h, and Iomanip.h).
  5. Input files (in the current example, "tape") used by your program, should be placed in the project folder.

Compiling the program

The program is compiled as follows:

  1. Open the Source Files folder by clicking the "+" symbol.
  2. Click the file containing the main function (in the current example, driver.cpp). This retrieves the text of the file into the right frame.
  3. Shortcut: click the compile icon.
    Alternately, click Build, Compile filename.cpp -- in the current example, Compile driver.cpp
  4. In the bottom frame, watch for error messages. If any appear, edit (in the right frame) and save whichever source code file needs correction, and go back to the previous step.

Running a program

Once you have successfully built an executable file, you can run or execute your program by

Shortcut: click the ! icon
Alternate: click Build, Execute projectname.exe (in the current example, Build, Execute cashreg.exe)

If your program creates an output file (in the current example, bill), you should find it in the project folder (after the program has run).

To page index
To Boxer's home page


Printing your console (i/o) window (Borland or MS Visual C++)

When you run your program, you may want a hardcopy of what appears in the i/o window (for example, to prepare your submission for a grade). This may be done by copying and pasting the i/o data to a word processor (such as Word) or a text editor (such as Notepad), then printing. (Notepad does a better job for many purposes, since, like the i/o window, it shows all characters with the same width, hence can demonstrate better than Word that columns of data are properly aligned.)

Windows XP

  1. When the console window (this is the DOS window used for i/o) is inactive, click the  C:\  icon that appears in the upper left corner of the console window.
  2. From the resulting menu, choose Edit, Mark.
  3. Stretch the cursor over the text in the console window that you wish to copy.
  4. Strike the Enter key to start a Copy operation (this is a shortcut - alternately, you could go to the Edit menu as in Windows 98 and choose Copy).
  5. Move the cursor to a Notepad or Word window, and paste.

Windows 98

  1. When the console window is inactive, click the "Mark" icon (the rectangle) on the console window.
  2. Stretch the cursor over the text you wish to copy.
  3. Click the copy icon.
  4. Move the cursor to a Notepad or Word window, and paste.

(Strange how the "progress" of a transition from Windows 98 to Windows XP results in a more complex process.)

It will often be the case that your i/o more than fills the console window.  In this case, you should arrange that your program pauses often enough that you can perform the above several times, for portions of the program's i/o.

When you have completing copying your data, you may print from Notepad or Word.

To page index
To Boxer's home page


Compiling C or C++ on cis.niagara.edu (Niagara's Unix computer)

The Gnu compilers have been upgraded, and the commands one uses with them are different than in the past.  The following information was supplied by Prof. Ann Rensel.  In the following, the "%" represents the operating system's prompt message (which may be different than "%"); what's underlined is the command you would enter.

C compiler

If you have a project with a single source file (say, "main.c"), you would compile it with

% gcc -c main.c and the resulting object file is named main.o

C++ compiler

Use ".cpp" as the extension of the file containing the "main" function.  In the following, we use the example of a source code file containing "main" with the file name "reciprocal.cpp".

For C++ the compiler is called "g++" - so if you had a program "reciprocal.cpp" then you would compile it by the command (this is the option students are less likely to prefer)

% g++ -c reciprocal.cpp  and the resulting file is named "reciprocal.o" (without quotation marks).

The -c option tell g++ to compile the program to an object file only; if you left it out g++ will attempt to link the program to produce an executable, whose name defaults to "a.out" (without quotation marks). To create an executable file from the object file, you would use

% g++ -o reciprocal reciprocal.o To create an executable file directly from the source code (this is the option likely to be more convenient for students), you can use % g++ -o reciprocal reciprocal.cpp The Unix option "-o" stands for "output" - if you use it, you name the output of the command. Here, using "-o reciprocal" means the output file of a successful compile will be named "reciprocal". As mentioned above, if you omit this option, using only % g++ reciprocal.cpp your executable file would be named "a.out".

When the program compiles successfully (via either of the methods described above), you would run the executable with

./reciprocal or ./a.out according to the form of your "g++" command.

To page index
To Boxer's home page