Lab 0: CodeWarrior C++ Instructions


IDEs

An IDE (integrated development environment) manages the programs that programmers write; they include at least an editor and compiler and often some analysis and management tools.

Most (probably all) IDEs provide you with an editor to enter the text of a program and save it in a file. Once your program is saved, you can translate it into your computer's machine language (the binary 0s and 1s that a computer understands) using the CodeWarrior compiler and linker. Graphically the process can be pictured as follows:

Starting CodeWarrior

Invoke CodeWarrior according to your instructor's directions. (This may involve double-clicking on an icon on your computer's desktop or choosing a menu item.)

Creating a Project

The CodeWarrior IDE requires that all programs be part of a project that simplifies the translation of a source program into machine language. Normally, we create a project first, before we create any source files for our program, although it is quite easy to add existing source files to a project.

To create a project, move the mouse to the File menu and choose New Project... from the menu. This should cause a dialog window named New to appear, and you should select the Project tab.

Look for the C++ Stationery projects for your operating system (e.g., on an Apple Macintosh, you'd pick Mac OS C++ Stationery).

Now select the folder where you want to save your project and code using the Location box. (There's a file-navigation button to the right of this box which makes selecting your folder easier.) For the labs, this should be a labs folder.

After you have the location selected, click in the Project name box, and type the name bases.mcp. It is a good idea to give the project the same name as your program, but CodeWarrior wants you to add the .mcp extension. You'll notice that bases is added to the location for the project. This is what you want. Click the OK button.

Now you have to pick what type of "stationery" project you want to create. You want a Standard Console. Your instructor will give you further help in selecting the exact type of project. When you have the type of project selected, hit the OK button.

A new project window named bases.mcp should appear. It may already include a few folders and files. In the Sources folder, delete the HelloWorld.cp file.

Creating the Source Program

Next, we want to create a new file named bases.cpp in which to store our program. To do so, move the mouse to the File menu, and choose File -> New Text File. A blank window will appear, named untitled. To give it the proper name, choose File -> Save and in the dialog box that appears, select an appropriate folder to save the file in, and enter bases.cpp (instead of untitled) as the file name. Finally click on the Save button. The window will then be renamed bases.cpp.

Now choose Project -> Add bases.cpp to Project, and bases.cpp will be added to your project. Your new files will not be added to your project automatically.

Within the bases.cpp window, enter (and personalize) the C++ source code from the main exercise. Hint: copy-and-paste works just fine from a browser into a editor window in CodeWarrior.

Personalizing the Program

The file bases.cpp is the work of a fictitious person, on a fictitious date, in a fictitious course, at a fictitious university. Edit the program's opening comment (the part between the /* and */ symbols) as appropriate to make it your work on the current date, in your course at your university.

Saving Your Work

Helpful hint: Many menu items have keyboard shortcuts and some may be found in a toolbar. Explore the IDE to find these tools to make the tedious jobs a bit easier and faster.

When the bases.cpp window contains the bases.cpp program, you can store this program in a file by choosing File -> Save. (Did you find a keyboard shortcut for this?) If you look in the your project folder, you will see that it is no longer empty, but full of a variety of files. Don't worry about these other files for the moment; they are just administrative files CodeWarrior uses to keep track of what is where and to supply libraries for your program. At all costs, don't delete them!

Compiling Your Program

When your source program is entered and saved, it is time to translate it into the computer's machine language. In CodeWarrior, this can be done using the Project -> Make menu item. When this is done, CodeWarrior displays a Building bases.mcp window in which you can watch the translation as it proceeds through the compilation and linking stages. If all is well, the window should disappear returning you to where you were.

If your program contains syntax errors (also known as "typographical errors"), CodeWarrior will display an Errors & Warnings window in which it will list the errors it discovered in your program. If you double-click on a given error, CodeWarrior will highlight the line in bases.cpp where that error was discovered. (Often, this is the line following the line where the error actually occurred.) CodeWarrior will not run a program that contains errors, so you must correct all errors in a program before proceeding.

To illustrate this, delete the last semicolon in the program, then use Project -> Make to rebuild your program. When the Errors & Warnings window appears, double-click on the error. CodeWarrior highlights the last line, even though the error is on the preceding line. This is because the Errors & Warnings window lists the line where the error was discovered. It wasn't until the compiler started processing that last line that the missing semicolon was noticed.

Fix the error, rebuild your program, and then continue.

Running Your Program

Running your program is just as easy as anything else in CodeWarrior. Choose Project -> Run, and the IDE will run the program for you. A window named Std C++ Console PPC.out will appear, in which your program will be executed.

When it prompts you for input, enter an easy-to-check number (like 15, 16 or 17) and verify that you get the correct results.

When the program finishes, you can quit the Std C++ Console program, usually without saving the output. (But read on for what you will want to do with that output.)

Printing a Hard Copy of Your Program

A paper copy of electronic information is called a hard copy; it is often useful to have a hard copy of source code. As with much of CodeWarrior, this is quite easy: make sure that bases.cpp is the active window and choose File -> Print. A Printer dialog box should appear. You should just be able to click its Print button to print a hard copy of your program's output. If you must do anything beyond this (such as specify a printer), your instructor will inform you of the details.

Printing an Execution Trace

It is often useful to be able to print a hard copy of the output from your program, especially for more complicated programs. Make sure that Std C++ Console PPC.out is the active window and then choose File -> Print.

Once you have a hard copy of your program's execution, you can tell your program to quit by choosing File -> Quit; a dialog window will then appear asking if you want to save the output from your program in a file. Since we do not need to save this output, click the Don't Save button. This is separate from quitting CodeWarrior itself, so make sure that you quit both your own program and CodeWarrior (eventually, not now).

Back to the Main Exercise

You should go back to the main exercise now and apply the scientific method to the code that you've now compiled and executed. Remember to bookmark this page so that you can re-use these instructions throughout this lab manual.

Return to this page when you finish the main exercise so that you can clean up the project for this lab exercise.

Wrapping Up the Session

Let's examine the final things you will need to do in most sessions.

Cleaning Up Your Project

As you can see in your project, a program may consist of source files and one or more libraries. In the course of writing and debugging a program, it may be necessary to compile the program dozens of times! Since all debugging is confined to the source files, recompiling the (unchanged) libraries each time would waste lots of time.

To avoid this inefficiency, once a file is compiled, a CodeWarrior project stores a copy of the compiled version called an object file. The Project -> Make menu item then recompiles any files that have been changed, and it uses the existing object files of any files that have not been changed. By avoiding unnecessary recompilations, this can save lots of time.

Object files also consume disk space. You can see how much by looking at the Code and Data columns in your bases.mcp window. To save disk space, the object files should be removed from a project any time you are done working on it.

To remove your object files, choose Project -> Remove Object Code. Click Current Target in the dialog that appears, and then watch the Code and Data columns in your bases.mcp window change to zeros as CodeWarrior removes the object files from the project and deletes them from your disk.

Terminology

IDE, compiler, hard copy, integrated development environment, linker, object file, opening comment, project, project window, syntax error, typographical error

© 2003 by Prentice Hall. All rights reserved.
Report all errors to Jeremy D. Frens.