Setting Up a Project "from scratch"
After you launch the CodeWarrior program, a menu bar will appear allowing you to create a project . Remember that project has a special meaning here: if you think of your C++ program as a car, the project would be analogous to its driver and support crew. A complete project includes a program you have written and library information which must be included with it in order for your program to run.
Setting Up Your Files and Typing in Your Program
Choose New Project in the File menu to indicate that you want to create a new project. Now, select the correct project stationery by clicking on the triangle next to MacOS, then the triangle next to C_C++, then the triangle next to Standard Console, then click on Std C++ Console PPC (or 68k if needed). Finally, click on OK.
Now you must type in a name for the new project you are creating. Let's say you are writing a program which converts Fahrenheit temperatures to Celsius. You would type in something like "ConvertTemps.mcp" for the project name. Choose the drive that you want to store your project on by clicking the Desktop button if necessary. Finally, click on the Save button which will create the project and save it on your disk. CodeWarrior creates and saves a folder for your project, named "ConvertTemps."
Now a new window will appear. This is the project window and will be titled "ConvertTemps.mcp." Click on the triangle next to Sources. Notice that a dummy program entitled HelloWorld.cp appears in the project window. We will deal with that later. You will also see two libraries included in the project: ANSI Libraries and Mac Libraries.
To type in your C++ program, select New under the File menu. A program window will appear in front of the project window. This is the window in which you will type and edit your program. Initially it will be named untitled. In this window, type in your C++ program. When you are done typing in the C++ program, you must use Save under the File menu to save it in the folder which contains your project, ConvertTemps in this case. Use the name "ConvertTemps.cpp" for your program file. Your C++ program file names must end with .cpp or .cp.
Take a look at the contents of the folder ConvertTemps. You will see 3 files: ConvertTemps.mcp, ConvertTemps.cpp, and HelloWorld.cp. You will also see 1 folder: ConvertTemps Data. Notice that CodeWarrior has created some of these files and folders for you automatically. The folder "Data" contains information that CodeWarrior utilizes while you work on your project.
ADDING and REMOVING programs in Projects
A complete project includes your C++ program and the library files. You must explicitly add your program to the project at this point, and remove the HelloWorld program, before you can go on.
First, to add ConvertTemps.cpp to your project, select ConvertTemps.mcp under the Window menu. This will activate the project window and place it in front of the program window. Now, click on the word Sources in the project window; which will cause the word Sources to be highlighted. Then, click on the program window, which will activate that window. Now, go under the Project menu and pick Add Window.
Next, activate the project window again by picking ConvertTemps.mcp under the Window menu. You will see that ConvertTemps.cpp is now listed in the project window under Sources. This means that your program was successfully added to the project.
Now, remove the dummy program. In the project window, click on the name of the file you want to remove. Here, if you click on HelloWorld.cp, you will see that name highlighted. Then, go under the Project menu and select Remove Selected Items. This will remove the file you have chosen.
Now you can proceed with compiling and running your C++ program!
A special note on data files: never "add" a data file to your project using the Add command in CodeWarrior's Project menu. Projects must contain only C++ source code.
Attachment - Using Metrowerks CodeWarrior C++
This project write-up is essentially a tutorial that will help you learn the basics of CodeWarrior, using instant project files given to you. It will help you get started writing C++ programs using Metrowerks CodeWarrior. In this procedure, you will type a C++ program, compile it, run it, and print out hard copies of the program and its output.
The CodeWarrior program is revised by the Metrowerks company about every 6 months. For this reason you may find that the version at the computing sites may not always be the same version the teaching staff has or you have. This handout is written for CodeWarrior Professional Release 3 using a Macintosh G3 or iMac at ITD computing sites. All versions are very similar, so you can always use this handout. Any variations you need will be discussed at the discussion section itself, or you can ask the teaching staff at office hours or via email.
CodeWarrior is not just a compiler. It provides an editor for entering your programs, a debugger, a runtime system, subprogram libraries, etc. CodeWarrior also contains compilers for the Pascal, C, and Java languages. In this course we will use only the C++ compiler.
Launching CodeWarrior and Entering a C++ Program
On the desktop of Mac workstations at Angell Hall, the Media Union and other ITD or CAEN computing sites, you will find an icon for a file server named Applications. Double-click on the Applications icon to find the Course Software folder. Open the Course Software folder and locate the CodeWarrior folder, which contains the Metrowerks CodeWarrior program. You could obtain CodeWarrior from this folder.
After you have copied a CodeWarrior file to your computer's hard drive, close all the folders you have opened on the Applications server. Find the icon for the self-extracting archive you copied to your hard drive and double click on it. That will open the UnStuffit program, which will extract CodeWarrior for your use. Click on the "Continue" button when it appears. When asked where you want to load CodeWarrior, select your computer's hard drive. You will then wait a minute or so while about 500 separate files are decompressed and stored on your computer's hard drive.
When the files are all decompressed, find the new folder labeled "Metrowerks CodeWarrior" and double-click on it. Inside you will find several icons, including one labeled "CodeWarrior IDE 3.1 §," which is the integrated development environment you will use to write, compile, run, and debug C++ projects. Double-click on "instant.mcp". CodeWarrior will automatically launch and a new window called "instant.mcp" will appear. This is the project window. Near the top of the project window, you will find three tabs, labelled "Files," "Link order," and "Targets." If the "Files" tab is not selected, click on it. Under the "Files" tab, you will find three entries. The first entry is for "Sources." To the left of the entry labeled "Sources" you'll find a small triangle. Clicking on this triangle will reveal another entry, labeled "instant.cpp."
When you double click on the line that says "instant.cpp", another window called "instant.cpp" will appear. That is the program window. File names ending in .mcp indicate project files, and those ending in .cpp indicate C++ source code files (C++ programs).
Whenever you launch CodeWarrior, a menu bar appears allowing you to work on a project . Project has a special meaning. A complete project includes a program you have written and library information which must be included with it in order for your program to run. The project window already lists several library files, under the headings ANSI Libraries and Mac libraries. These files will be part of every project you work on in this class.
You can now start typing your program into "instant.cpp." Replace the instant program with the program given on the last pages of this write-up, exactly as you see it, including comments, spacing, and capitalization. On the screen, you will see different colors used for different program elements (reserved words, comments, etc.) .
While typing, use TABs or spaces to indent, and hit RETURN at the end of each line. It is generally best to use TABs to line up items vertically. CodeWarrior will do some automatic indenting for you.
On this project, if you type in the given program exactly as it is written, you will have no syntax errors (errors in C++ grammar). If you make any mistakes, you can correct them by backspacing and retyping. Basic editing techniques can also be used, and the standard Cut, Copy, Paste, and Undo functions are available under the Edit menu.
You can save a revised copy of your program at any time by choosing Save from the File menu.
Checking and Compiling your C++ Program
When a program is compiled, it is translated into object code. Some types of syntax errors prevent the compiler from making a translation, and will not be found until you ask CodeWarrior to check the syntax. To do this, choose Check Syntax under the Project menu. If there are syntax errors, you will see a message window which lists the errors and tells you what lines they occur on. You can click on the stop sign next to a particular error line and CodeWarrior will then display an arrow pointing to the line in the code which contains that error. Sometimes an error occurs not in the line pointed at, but in some line above it, so you may have to look at previous lines to locate the error.
Edit the program until there are no syntax errors remaining. When you have no syntax errors left, compile the program by selecting Compile under the Project menu.
Running the Program
When you run the instant program, a new window will appear on the screen, with the name Std C++ Console PPC.out. This window will display the actual output produced by the program, and we will call it the output window.
Run the program (i.e. execute the object code) by choosing Run from the Project menu. When you are prompted for your input values, type them in and hit RETURN after each one. You can use the data values shown in the sample output given right after the program handout. Notice that the output window displays both your input and the program's output. In this project, you are entering the data interactively.
When you run your program, the output window is displayed and the menu bar at the top of the screen will contain only a File and an Edit menu. When the program run is completed (i.e. you have typed in the input values and seen the resulting output) you must then choose Quit under this File menu to end the run. You will be asked if you would like to save the output ("do you wish to save changes to Std C++ Console PPC.out ?"). Generally this does not need to be saved. You can print your output by choosing Print from this menu before you select Quit.
If you type in the program correctly but then enter incorrect input values during a run, errors may occur. You must re-run the program in this case until the input values are correctly entered, and the correct output is produced. If the program is not typed in correctly but it still runs, it may produce the wrong output. If this occurs, edit the program to fix any errors, then use Bring Up to Date under the Project menu to re-compile the program before you run it again.
Once your program runs correctly, print out copies of the program and its output to turn in for credit.
Take a look at the folder instant Ÿ at this point. You will see that CodeWarrior has automatically added some files and folders for you.The only two files you need to open and work on yourself are your prog ram (.cpp) file and your project (.mcp) file. Do not delete the other files though, as CodeWarrior uses them while you work. Whenever your start working on one of your programs, open the project file first.
Printing Your Program and Its Output
You must print both the program window and the output window so that you can turn in a listing of your program and the results of the run. To print the program, first make the program window active by clicking in it, and then select Print from the File menu. You will see a dialog box allowing you to choose various printing options.
To print the output window, choose Print under the File menu while the output window is displayed.
Quitting CodeWarrior
Choose Quit from the File menu when your program or project window is active to exit the CodeWarrior program. You will return to the Macintosh desktop.