C++ Intermediate
Volume Number: 6
Issue Number: 7
Column Tag: MacOOPs!
C++ Intermediate Code
By Dan Weston, Portland, OR
Note: Source code files accompanying article are located on MacTech CD-ROM or
source code disks.
[Dan Weston is a long-time Macintosh developer and author. His new book,
Elements of C++ Macintosh Programming, will be published by Addison-Wesley in
June 1990.]
What is C++ Doing?
C++ is a new version of C with object-oriented extensions. Most versions of
C++ are currently implemented as preprocessors. That is, you write C++ source
code, then the C++ preprocessor (called CFront) takes the C++ code and translates it
into standard C code. The resulting C code is then fed to a regular C compiler for
compilation into executable code. The programmer usually doesn’t see the
intermediate C code produced by CFront since the whole process is wrapped in a script
(called CPlus in MPW) that invokes CFront and then pipes the intermediate code
directly to the C compiler.
This article will look at some examples of intermediate code to give you a better
idea of what CFront is doing to your C++ code. Knowing what the intermediate code
looks like will help you to understand the cryptic error messages that you sometimes
get during a build of a C++ program and will also help you to avoid certain C++
constructions that result in inefficient C code.
How can you Spy on C++
The first thing to know is how to get a look at the intermediate code generated by
CFront. If you pass the ‘-c’ option to CPlus, it will write the intermediate C code to
standard output without invoking the C compiler. You can redirect the output stream to
a file to capture the intermediate code for further inspections, as shown by the
following MPW command.
#1
CPlus test.cp -c > test.cp.c
The intermediate file will be filled with information from any header files that
you have included in your C++ code. You can usually ignore most of the header