You are on page 1of 7

2/23/13

Using xCode in Mac OS X for C++ Programming

Writing and Running C++ Programs in the Mac OS X 10.4 Environment using x o e Cd
JL Popyack January 2005

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 c C compiler. c Likewise, C++ programs may be run using the C compiler. Furthermore, the GNU compilers are included C with the UNIX installation, so that the g cand g +compilers are available for compiling C and C++ c + programs, respectively. Follow this link for directions for compiling with g++ (h t s / w w c . r x l e u ~ c 1 1 W 0 / x r s g + i d x h m ). tp:/w.sdee.d/ms7/i7eta/+/ne.tl 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 x o eenvironment in OS X 10.3 (and the Project Builder Cd environment in previous versions of OS X). The x o eenvironment provides a graphical user interface for Cd the gcc compiler and the UNIX execution shell, which means:
x o eprovides a GUI editor (similar to the program editor in Visual Studio) Cd x o eprovides an interface that includes a project into which program files are loaded Cd x o eallows the user to compile a program and displays any error messages generated Cd x o eallows the user to run a program and provides an output window with the results. Cd

The directions below describe how to create, edit, compile, and run a C++ program in the x o e Cd environment. Since x o ewas derived from Project Builder, the directions for using Project Builder are Cd very similar. I. Open x o eby finding it in the Applications window on your Macintosh. Your first duty will be to Cd 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 select "Tools -> C++ Tool" to create the project.

https://www.cs.drexel.edu/~mcs171/Wi07/extras/xCode_Instructions/index.html

1/7

2/23/13

Using xCode in Mac OS X for C++ Programming

2. 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.

https://www.cs.drexel.edu/~mcs171/Wi07/extras/xCode_Instructions/index.html

2/7

2/23/13

Using xCode in Mac OS X for C++ Programming

3. 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 fullyfunctioning 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.

https://www.cs.drexel.edu/~mcs171/Wi07/extras/xCode_Instructions/index.html

3/7

2/23/13

Using xCode in Mac OS X for C++ Programming

4. When you are ready to get down to business, you should select F l - S v A . .and give this file ie>ae s. another name. In our example, that name is "myProgram.cpp". We can actually think of much more clever names, but are resisting the impulse.

https://www.cs.drexel.edu/~mcs171/Wi07/extras/xCode_Instructions/index.html

4/7

2/23/13

Using xCode in Mac OS X for C++ Programming

5. 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 m i . p in your program even after you decided to get rid of it. ancp

https://www.cs.drexel.edu/~mcs171/Wi07/extras/xCode_Instructions/index.html

5/7

2/23/13

Using xCode in Mac OS X for C++ Programming

6. What now? How about editing m P o r m c pso that it contains a working C++ program. We yrga.p know you will find the urge to write a "Hello, World!" program irresistible. A variant appears in the example below.

7. 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 selfexplanatory. 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.

https://www.cs.drexel.edu/~mcs171/Wi07/extras/xCode_Instructions/index.html

6/7

2/23/13

Using xCode in Mac OS X for C++ Programming

In summary, the x o eenvironment provides an integrated development environment for Macintosh Cd programmers that is intuitive to use and makes use of the acclaimed GNU compilers. Who could ask for more?

https://www.cs.drexel.edu/~mcs171/Wi07/extras/xCode_Instructions/index.html

7/7

You might also like