Building Applications in
CodeWarrior Pro 8
by Jen Golbeck, Winter 2001
revised by Mike Hasak, Winter 2003
In CodeWarrior, code is managed as a "project". A
project is a collection of C++ files that will be combined into producing one executable
as follows:

To build a new standard application
1. Launch
CodeWarrior. The icon is located on the hard drive in Programming:CodeWarrior
Pro 7:Metrowerks CodeWarrior and is called CodeWarrior IDE 4.2
2. When CodeWarrior launches, all you will see is the set of pull
down menus at the top. A default page or application will not show up. To write
a program you will need to create a project. This holds the files, the
executable, the debugger, and other necessary information. To do this, go to the
file menu and select "new..."

3. You will be prompted to create a project. In the window on the
left, select Mac OS C++ Stationery. On the right, type in the name of your
project. This will create a folder containing all the data in the location selected
in the ÒLocation:Ó field. You should make sure to either create this folder on a
disk of yours or to make a copy of it and take it with you. The hard drives are
wiped clean every night in the maclab, and there is no way to recover code that
you wrote and did not take with you.

4. You will then be
prompted to select the type of project. Choose Mac OS Carbon then Standard
Console then C++ Console Carbon.

5. CodeWarrior will create a project, and
will open a window that contains all of the project information. You should be
primarily concerned with the sources folder under the "Files" tab.
This shows what files are going to be compiled as part of your program. By
default, a "Hello World" program will be there. You can view this
code by double clicking on the name of the file.

This file is often a good starting place,
something to use as a template. However, you may want to start with your own
code from scratch.
The carb.r file is part of the Carbon
stationery (code that tells Codewarrior how to compile for the Mac OS) Ð donÕt
throw it away, but you can safely ignore it.
6. To start with
your own code, the most important thing is to first remove the HelloWorld.cp
file from the project. Since any C++ program can have only one main function,
you want to remove this default file so your code will execute properly. To do
this, simply click on the file name in the project window, and drag it into the
desktop trash. A shadow of the file outline will follow your mouse to the
trash.
7. To create a new
file from scratch, go to the file menu and select "New Text
File". This will create
a blank file that you edit in the CodeWarrior simple text editor.

8. Having entered your code, you will want to save it. You should
save this file in the folder created for your project. You chose this location
when you created the project in step 3. Once your file is saved, it is still
not included in the project. You will need to add it to the project window. The
simplest way is to drag the file from its folder on the desktop into the
project window. When your mouse enters the project window, a black line will
appear indicating into which folder the file will be dropped. Move your mouse
so that the line is immediately under the sources folder. Then drop the file
there. Now, when you look in the sources folder in the project window, your
file should appear there. Alternately, you can go to the project pull-down menu
and select Add Files. Select your new file, and click Add.

In either case, you will be able to view
it by simply double clicking on its name in the project window.
You may also take code that you have
written in other plain text editors and add them to your CodeWarrior project in
the same manner.
9. You may add as
many files to the project as you like, but remember that a project only can
contain one application. That means only one file can contain a main function.
If you want to create separate programs, you need separate CodeWarrior
Projects.
10. To compile your application and to check the syntax there are two
options. Make (
) will
check syntax and create an application. Run (
) will
do everything Make does, plus launch the created application. To do either of there,
go to the project menu and choose "Make" "Run" (or hit
apple-M or apple-R respectively on the keyboard). CodeWarrior will start to build the project by first
checking the syntax, and pointing out any errors. If all of the code is
correct, it will create an application for you and, if you have chosen Run,
launch that application. Note that all of your files will be automatically
saved when you compile an application.

11. If there are
errors, you will be given a window that lists the expected error, as well as
the line and file in which it occurs.

There are two categories of problems:
errors and warnings. Errors are syntactic errors that must be corrected for the
code to compile. Warnings are things that CodeWarrior things might be wrong,
but which will not prevent the code from compiling.
The window above shows several things to
keep in mind. The actual error in the code is that there is a semicolon
missingfrom the end of the line
cout << "Hello
World, this is CodeWarrior!" << endl
However, the debugger points out that the
error is on the next line. The lesson is this: debuggers and syntax checkers
aren't perfect. CodeWarrior cannot fix your code for you (nor can our tutors).
You should think of the syntax checker as a tool to help you find errors, not
as something that should be correct in pointing out every error of your code.
Additionally, you may sometimes see hundreds of errors appear. Often there is
one syntax error that makes it impossible for CodeWarrior to understand the
rest of your code. It is to your benefit to fix one error at a time and then
re-compile. In many cases, fixing the first error eliminates many others as
well.
Debugging is without a doubt the most
frustrating process in coding. This is a tool to help , not to do that
work for you.
You may correct errors in the lower
section of the Errors and Warnings window. This will automatically change them
in the file. A red arrow points to the line where CodeWarrior things the error
occurs, and the text is highlighted. If you have multiple errors, clicking on
the next error brings up the file and location of that error in the lower
window.
12 . Once you have
successfully compiled your code, it can be run. This launches a separate
application that contains your working code. You may switch back and forth
between that application and your code in CodeWarrior, however if you want to
recompile your code, you must first quit the running application.
The debugger in CodeWarrior allows you to move step by step
through your program to see which function calls are being executed, what each
state is of member variables, and how loops and single lines are being
executed.
1. If
you go back to the project menu, you'll see that above the "Run"
option is "Debug". Select that, or use the apple-R keystroke indicated
in the menu, or use the green-bug button in the project window (
).

3. After
checking for syntax errors, the debugger window will appear.

The buttons across the top provide you with your options. From
left to right they are
* "Play" which will execute your code until it reaches a stopping
point (see step 4)
* "Stop" which stops execution
* "Kill" which kills the program and brings you back to the project
window
* "Step Over", which executes a line and moves to the next line (not
showing you the internals of any functions that are called)
* "Step Into" which executes the current line, and shows you step by step
any sub-functions being executed.
Try this to see how involved this whole libraries business is.
* "Step Out" which brings the debugger up one level in the execution of
sub-functions or phrases.
In the source window is a blue arrow pointing to the line about to
be executed. You may move that arrow to any line at any time to re-execute a
statement, or to skip to a specific section of code. The Variables window on
the top right shows any variables and their current value. The stack window
shows functions being executed.
4. Stopping
points are markers that tell the debugger to stop executing if you have clicked
on Play. If
you have isolated a section of your code which you think may have problems, you
can set a marker there to indicate that all the code before it should be
executed, and when you reach this point, execution in the debugger should stop
and you should be able to step through it with the Step buttons defined above.
By clicking on the grey
field to the left of your code in the code editor, a red dot will show up. To
remove a red dot, click on it again. You may set as many or as few stopping
points you like Ð the line adjacent to a red dot will be denoted as a stopping
point.

This is a stopping point. Simply click next to the line you want
to stop at, and then run the debugger.
Other Features of CodeWarrior
As you may have noticed, CodeWarrior provides some nice features like color-coding. There are many, but a few in particular will make coding much easier.

Across the top of the code editor window
are five buttons; the first three are particularly useful. The last two control
syntax coloring schemes and versions. Of the others, from left to right...
Header Files - This button
lists all of the header files included in your project. It's a big list because
of all the default system headers. If you click on this button, you can see the
list and select a header. That header will then open in a new window. This is
useful if you have included your own headers and want to open one for reference
or correction.
Functions - This button
lists all of the functions in the current file. You can use it to select a
function and jump to that part of the code. In long files, this is a great time
saver. Note that it will not jump to function prototypes, only function
definitions.
Markers - This button allows you to set or view markers. It
is very similar to the Functions button in that it will jump you to a specific
place in the code. However, you can place a marker anywhere in the code you
like, and give it any name you want. This is useful if you have several classes
in one file, if you have prototypes and then definitions, or of there is a
piece of a long function that you keep coming back to.