CodeWarrior Pro Introduction (Macintosh Version)


Revision History:

Written for CodeWarrior Pro I (Macintosh) by Brad Pliner - Sept. 1997.
Adapted for HTML, upgraded for CodeWarrior Pro II (Macintosh and Windows) by
JL Popyack - Jan. 1998.


This lab was created with CodeWarrior Professional Release 2 in mind. All of these topics should relate easily to other versions. Except for minor differences and where identified specifically, these topics apply to both the Windows and Macintosh versions of CodeWarrior. Be sure to go over the topics covered in this lab on your own machine, with your own copy of CodeWarrior.

This lab is meant to be both as a tutorial and a quick reference.

 

Introduction to the Metrowerks Environment

Metrowerks CodeWarrior Professional Release 2 is an integrated software development environment. It runs on either the Macintosh or on Windows 95/NT. The platform you run CodeWarrior on is called the host platform. You can compile your software to run on Mac OS (68K or PPC), Windows 95/NT, or on a Java virtual machine. This is called the target platform. CodeWarrior supports various programming languages, including Pascal, C, C++, and Java. There are also other versions of CodeWarrior supporting other host and target platforms.

CodeWarrior distinguishes files by their names. In particular, it expects that the names of files it uses will end in a period (.) followed by one or more characters called the file name extension. Here are some common file name extensions:

.mcp

Used for "project files". This has changed from previous versions.

.rsrc

Macintosh resource file for adding windows, etc. to your Mac program

.1ib

Library files

.c

C source code files

.cp

C++ source code files. Sometimes .cpp and .cxx are sometimes used as well. On UNIX, ".C" is used, but the Mac and Windows platforms do not distinguish between capital and lower case characters in file names.

.p

Pascal source code files. (Also .pas)

.java

Java source code files.

For example, you might pick the name hw1.cp as the name of a C++ file for your program. You could choose the name hw1.mcp as your project file.

When you write a program you need to have one project file, and any number of source code files. When you are ready to compile your program, the compiler turns your source code into machine instructions (object code). In this version of CodeWarrior, the object code is all stored in a "data" folder, instead of inside the project file. Finally, Metrowerks links the program pieces (plus any parts of any libraries specified in the source code) together with its linker, and a complete executable file is generated. When you make changes to your source code, Metrowerks only recompiles those files which have changed. This is a huge time saver when you make a small change to a huge project -- assuming that you have broken your program into chunks that can be placed in a number of separate files.

In this first laboratory, we will have only one source code file, and one project file. However, it won't be long before we will be urging you to use more than one source code file.

 

Creating a New Project

Here's how to create a new program on Metrowerks CodeWarrior Pro 2.

1. Start up CodeWarrior IDE. (IDE stands for "integrated development environment".)

2. Select New Project from the File menu. You should see the following dialog box. On your lab sheet, record any differences between the dialog box you see and the one shown below:

Project Stationery files are project configurations that are already set up for you. You simply need to choose a project stationery file, and then add your own files to it. The project we will use is Mac OS: "C/C++:ANSI C++ Console PPC". To find this option, click on the triangle next to "MacOS". The dialog box should appear as below. Again, on your lab sheet, record any differences between the dialog box you see and the one shown below:

3. Select C/C++ and find the "ANSI C++ Console PPC" stationery. You may need to scroll down to find this. Once again, on your lab sheet, record any differences between the dialog box you see and the ones shown below

NOTE: Before selecting OK, make sure that the "Create Folder" checkbox is selected. This will put your project into a new folder.

4. Once you have specified what kind of project you want, you will see a dialog box with a line that you can fill in for the project name. Select the directory that you want the project to reside, by manipulating the directory box. Type in the project name, and hit return to create the project file in the specified directory. Be sure to end the project name with .mcp. Let's call this project "labl.mcp". On your lab sheet, indicate that you have completed this step.

5. You will now see a listing of the pieces of your new project. For the PowerPC C++, it looks like this:

The window is broken into groups. Above, a group has been created for Sources, for ANSI libraries, and for Mac libraries. Library files contain many procedures for us to use in our programs, without having to program them ourselves. Plus they are precompiled, so they only take a second or two to link into our program. You can create your own groups if your project becomes large.

The dot in the row for HelloWorld.cp means that the CodeWarrior debugger is enabled for that file. The debugger is a feature of the CodeWarrior development environment that helps you figure out what a program is doing (or is doing wrong) while it is executing. If the debugger dot is not on, you will see assembly code instead of C++ code when debugging your program.

 

Project Preferences

From the Edit menu, select the menu for setting project settings. The menu will have the name of your project. You will see a dialog box. Select the C++ warnings. Here's what you should see:

Turn all warnings on! (Leave "Treat all Warning as Errors" turned off) This will tell you when the compiler thinks there may be an error in your code, even though it is syntactically correct.

Tip for Mac OS Users: Select "Show Balloons" from the Guide menu and position the mouse cursor above each button to see what all the compiler options do!

If you click on the C/C++ language settings (on the left), you will see options specific to the C/C++ language. Use help to look at the various options, but the default settings are good.

You can speed up your program by turning on optimizations. The more optimizations you turn on, the slower your program will compile. Optimization is useful for programs you are going to publicly release, or actually plan on using. For homeworks, the speed of execution is not important, and you will probably not notice the speed gains from optimizations. If you plan on using optimizations, leave them off until you are ready to ship your product. Be sure to thoroughly test any software you plan on releasing publicly!

On your lab sheet, indicate that you have completed this step.

6. Note that the toolbars at the top of your project window allow you to quickly make or run the project. On your Macintosh, you can learn the function of each of these buttons by using "Show Balloons" as above. In Windows, you can simply position the mouse cursor above the button and wait until the description appears.

On your lab sheet, record the title of each button. Which button do you press to run your program?

7. Running a program provided by Metrowerks. Let's now compile and run the program. Either select Run from the Project menu, or click the appropriate button on the toolbar. A status window will appear, that shows the progress of the compilation. Eventually a new window will appear where input and output of your executing program will appear. On your lab sheet, record the title of thewindow that appears, and the output that appears in the window. (If the output is especially wordy, you need only record the first few lines.)

The running program is a new application that has been launched. When finished, you should close the application. To do this on the Macintosh, you must choose "Quit" from the "File" menu. You will have the opportunity to save your output first. In Windows, you simply need to hit the "Enter" key.

8. After running the program, return to CodeWarrior and find your project window. Double Click the HelloWorld.cp in the project window. Select Save As in the File menu. This allows you to rename the file, and it will automatically replace the file in the project. Save the file as "lab1.cp". On your lab sheet, indicate that you have completed this step.

9. Entering a program known to be error-free. You may now edit the file to write your own program. Choose Select All from the Edit Menu, and then hit delete to erase everything. We can now start typing in our C++ program. For the purposes of the laboratory, type in the program listed at the end of this handout, entitled "A Sample C++ program". Save the file when you are done. Notice that the comments are in red and the keywords (identifiers that have a special purpose in C++ and cannot be used as variable or constant names) are in blue. You can close your file and reopen it simply by double clicking its name in the project window.

Recall that in C++ comments start with the characters // and are not part of the instructions to the computer. Nevertheless we expect that all programs that you write will have comments, as they are invaluable to whoever reads the program (the grader, the instructor, a coworker, or as a reminder to yourself at a later date).

10. It is very possible you have made an error when typing the program. If so, when you try to run the program, the "Errors & Warnings" window will appear and will contain error messages. You will need to make corrections, paying close attention to the differences between the sample program and your program. The compiler will identify the first item it found when detecting an error.

If you get error messages, record the first error message on your lab sheet. Determine what the error was in your source code, and correct it. Explain the error on your lab sheet. Continue this process until the program runs correctly.

When the program runs correctly, a window will appear in which you are prompted for a temperature in Fahrenheit. After entering the temperature and hitting the "Return" key, the value will be converted to Celcius and the result printed. Record your input and the result on your lab sheet.

11. When the program has finished, go to the File menu and select Quit to end your program execution session. (In general, saving the window is a good idea if you want to print out the program's results, or include them in a document you are preparing. The text output may be required by instructors, to demonstrate the correctness of your program.)

12. Introducing deliberate errors. Open your source file again by double clicking on its line in the project window.

12. Modify your file and put an intentional error: remove a necessary semicolon. Run the program again. Because CodeWarrior is aware you made changes to your source code, it will try to recompile and relink.

13. UH OH! You should have gotten a compile-time error message. Compare what the error message says to what the problem really is. Not all error messages are very accurate or descriptive. Double clicking on error messages will bring you to where CodeWarrior thinks the error exists in your source code. Fix the error, but try other kinds of modifications (one at a time) and see what happens. In each case, enter the results on your lab sheet.

a) Delete a line declaring a variable

b) Remove a "//" from the beginning of a comment line

c) Add a "//" to the front of the void main() line, turning it into a comment.

d) Change a + to a #

e) Delete the line containing the compiler directive #include <iostream.h>

14. Now fix the program back to its original working state and run it again. But instead of typing in a number, type in a word such as "dog". What happens? Enter the results on your lab sheet.

 

A Sample C++ program

#include <iostream.h>
// A very short program that, when executed, asks for a number, 
// and prints out a number.  The number input is regarded as a 
// temperature in Fahrenheit, and the number output is the 
// corresponding temperature in Celsius.
 
void main()
{
	double fahrenheit, celsius;
 
	// Constants used in temperature conversion.
	const double fahrenheitBase = 32.0;
	const double F2CScale = 1.8;
 
	cout << "Please enter a number (temperature in Fahrenheit): " ;
	cin >> fahrenheit;
 
	celsius = (fahrenheit - fahrenheitBase)/ F2CScale;
 
	cout << "the corresponding temperature in celsius is:" 
            << celsius << " ." << endl;
}

 

See Also:

CodeWarrior Pro 2 Installation Instructions for Mac OS
CodeWarrior Pro 2 Installation Instructions for Windows
CodeWarrior Pro Debugger Tutorial