C++ And Xcode

Alpaca

Registered
*n00b question*

I am currently reading a nice O'reilly guide to c++. Thing is, I need a c++ compiler, and and ide would help, too.

So, I saw my xcode CD sitting on the shelf, installed the program, and was very happy, until I realized that I have no idea how in the world to compile a c++ program. So, here's what I need:

I need a way to simply compile standard, single, .cpp files. Like hello world. What do I need to do, from double-clicking the xcode icon, to running my program?
 
I suspect xcode will do this for you, if you can sort out how to get it to make a build target and so on, but I'm not familiar with xcode as more than a nice editor (which is to say, I don't know about most of the cool stuff xcode can do for you). I can tell you the standard unix-y way of doing it, which has the advantage that it's the same on linux, solaris, bsd, etc etc etc.

When you install the developer tools, you get the compilers, the headers, everything you need to get going. To compile a .cpp file, just run the command

c++ foo.cpp

This will give you a program called a.out. If you want a more useful name, you can

c++ -o foo foo.cpp

To get a program called foo. To run it, just

./foo

The O'Reilly book should have a section on compiling (?), maybe even a quick overview of the most common options of the gcc - which is what you have on OS X.

If you're going to do any sort of developing at all, you're going to use makefiles - it'll save you a lot of hassle. I won't go into that, there are lots and lots of intros to makefiles out there; google is your friend.
 
1.) File > New Project...
2.) C++ Tool

To build the project, click the Build button. :D

If you want to run the project outside of Xcode, you probably need to go to Build > Show Detailed Build Results and choose Deployment from the pull down menu.

There's probably other ways to do it too. But that works for getting started with command line programs.
 
Back
Top