Odds and Ends
Volume Number: 10
Issue Number: 8
Column Tag: Getting Started
Universal headers and code building under THINK & CodeWarrior for
68K and PowerPC
By Dave Mark, MacTech Magazine Regular Contributing Author
Note: Source code files accompanying article are located on MacTech CD-ROM orsource code disks.
Last month’s column walked through our MDEF and the test program that brought
it to life. When I first wrote the MDEF and Tester, I did all my work using THINK C,
version 6. Before the column went to press, I installed THINK C 7, as well as
MetroWerks CodeWarrior, Developer Release 2 (more commonly referred to as
DR/2). My goal was to develop a single set of source code that would build properly in
both environments. I learned a lot along the way. That’s what this month’s column is
all about.
We’ll look at porting code from THINK C 6 to THINK C 7. Next, we’ll go over the
changes you’ll need to make to get all the Primer, Volume I programs running under
both THINK C 7 and CodeWarrior. Finally, we’ll walk through the process of creating
projects using CodeWarrior. We’ll generate both 68K and PowerPC versions of last
month’s MDEF and Tester programs.
Moving From THINK C 6
to THINK C 7
Seems like every time Symantec comes out with a new version of their C
compiler, I end up having to make a bunch of changes to my source code.
In writing a book, that can be a real pain, since I have no way of getting the
source code changes to the people who have already purchased the book and then
upgrade their compiler. Bottom line, if you know someone who has a copy of the
Primer, please pass this column along to them. Better yet, help support MacTech and
tell them to get a subscription...
The move from THINK 6 to THINK 7 was no exception. In the past, the changes
were usually brought about by changes in the level of type checking mandated by the
compiler. When I moved my code from THINK 5 to THINK 6, I ended up adding a bunch
of typecasts to various Toolbox routines, casting the parameters so they matched the
function prototypes in the Toolbox include files. Adding the typecasts was a pain, but it
definitely made my code much better. Strict type checking is important. If your
parameter types don’t match, strict type checking forces you to explicitly cast your
parameters to the right type if they aren’t already of the right type. This helps you
avoid a lot of simple mistakes, like passing a pointer to a short when you meant to pass
a pointer to a long.
THINK 7 kept the strict type checking imposed by THINK 6. Much more
importantly, THINK 7 replaced the THINK 6 Toolbox include files with Apple’s
standardized universal headers.
Under THINK 6, you’d get to the Toolbox include files by diving into the THINK C
folder, then into the folder Mac #includes, then into the folder Apple #includes. There
you’ll see files like Dialogs.h and Quickdraw.h. Under THINK 7, the folder Apple
#includes has been renamed (Apple #includes), the parentheses hiding the folder
from the normal search path. In the Mac #includes folder, there is now a folder named
Universal Headers, which contains most of the same files as (Apple #includes), but
with some important changes, which we’ll get to in a bit.
Under CodeWarrior, the universal headers are found in the MetroWerks C/C++
folder, inside Headers , inside Universal Headers . Note that while THINK C keeps all
its headers in the same folder, CodeWarrior splits some of their headers off into a
folder called System Extras , which is in the MetroWerks C/C++ folder. I prefer
the THINK C method, since I only have to go to one folder when I want to browse through
a header file. On the other hand, maybe there’s a benefit to the CodeWarrior approach.
Anyone have any opinions on this? Just curious...
One of the benefits of the universal headers is that, by using them, you’ll have a
much easier time writing source code that compiles under all three of the major C
compilers: THINK C, CodeWarrior, and MPW.