Writing and Running C++ Programs in the Mac OS X 10.4 Environment
Sophisticated students with refined palates may find themselves using Apple
Macintoshes, and wish to compile and run C++ programs on their machines. Because
Microsoft's Visual Studio only runs in the Windows environment, however, it
is impossible to use Visual C++ directly on a Macintosh. We should point out
that by installing VirtualPC, a Macintosh user may create a Windows
simulator on their machine and thereby run Microsoft Visual C++. However, VirtualPC is
quite memory intensive, and since it requires both an installation of Windows
XP and Visual Studio, the user may find that extra memory is required and execution
speed is still too slow to make this a viable solution.
Fortunately, there are other possibilities for a Macintosh user who wants
to compile and execute programs in ANSI Standard C++.
First of all, OS X is written with a UNIX BSD kernel, which means that the
C language is an integral part of the operating system, and C programs may
be run from the Terminal window using the cc C
compiler. Likewise, C++ programs may be run using the CC compiler.
Furthermore, the GNU compilers are included with the UNIX installation, so
that the gcc and g++ compilers are available for compiling
C and C++ programs, respectively.
Follow this link for
Note that none of the aforementioned compilers provides a graphical user interface
(GUI) however, nor do they provide an integrated enviroment in which
programs can be compiled, linked, and executed.
Apple has come to the rescue by providing the xCode environment in
OS X 10.3 (and the Project Builder environment in previous versions of OS X).
The xCode environment provides a graphical user interface for the
gcc compiler and the UNIX execution shell, which means:
- xCode provides a GUI editor (similar to the program editor in Visual
- xCode provides
an interface that includes a project into which program files
- xCode allows the user to compile a program and displays any error
- xCode allows the user to run a program and provides an output
window with the results.
The directions below describe how to create, edit, compile, and run a C++
program in the xCode environment. Since xCode was derived
from Project Builder, the directions for using Project Builder are very similar.
- Open xCode by finding it in the Applications window on
your Macintosh. Your first duty will be to create a new project. Do this
by choosing "New Project" from the "File" menu! An "Assistant" window
should open that allows you to create the project (see below). You should
"Tools -> C++ Tool" to create the project.
- Next, you need to create a project. In our example below, we have given
it the name "myProject". The default path "~/myProject/" appears.
You can change this, but it makes sense to use the default.
- In the "myProject" window, you will see that a program named "main.cpp"
has been created and installed in the project by default. (Sometimes these
wizards are a little too helpful, if you know what I mean.) This program
almost certainly does not do what you want it to do, but it is a fully-functioning
C++ program. This means you can compile and run it right now. Go ahead, if
you must. We'll wait for you to get it out of your system.
- When you are ready
to get down to business, you should select File->Save As... and
give this file another name. In our example, that name is "myProgram.cpp".
We can actually think of much more clever names, but are resisting the impulse.
- Notice that after saving the program with a new name, the new program is
installed in your project, and "main.cpp" is pretty much forgotten.
This stands in contrast to Microsoft Visual C++, which keeps main.cpp in
your program even after you decided to get rid of it.
- What now? How about editing myProgram.cpp so that it contains a
working C++ program. We know you will find the urge to write a "Hello,
program irresistible. A variant appears in the example below.
- Notice the icons in the toolbar that say "Build", and "Build and Go". The
simplicity of this interface makes one wonder what all the other compiler designers
are thinking. In case it is not obvious, clicking the "Build" icon will build the
project -- that is, it will compile the program file(s) and link the code for
any other included libraries, producing an executable ("double-clickable")
file. Clicking the "Build and Go" icon will not only build the project, but
run the program! Notice that in the window that appears, you also have access
to various debugging tools. These are fairly self-explanatory. In the figure
shown below, the sample program has been executed, the user has entered data
when prompted, and output has been produced. The output can be saved in a file
by selecting "Save" from the "File" menu.
In summary, the xCode environment provides an integrated development
environment for Macintosh programmers that is intuitive to use and makes use
of the acclaimed GNU compilers. Who could ask for more?