Writing C++ Programs on Mac OS X

A frequently asked question on Mac programming forums is “Can I write C++ programs on Mac OS X?”. Yes, you can write C++ programs on Mac OS X.

Install the Xcode Tools

The Xcode Tools contain everything you need to write Mac OS X applications, including C++ programs. Every copy of Mac OS X contains the Xcode Tools. If you have Mac OS X 10.4, there should be an item called Xcode Tools on the Mac OS X DVD. This item contains the installer. Older versions of Mac OS X that ship on multiple CDs contain either an Xcode Tools or Developer Tools CD.

Apple frequently updates the Xcode Tools so the version that shipped with your copy of Mac OS X is most likely not the most recent version. If you have a broadband Internet connection, I recommend going to Apple’s developer page and signing up for a free ADC membership. After signing up for an ADC membership, you can download the most recent version of the Xcode Tools.

One common concern people have when installing the Xcode Tools is the size of the install. The easy install of the Xcode Tools is over 1.5 GB, which is overkill if all you want to do is learn C++. You can perform a custom install of the Xcode Tools to reduce the size of the install. If you are low on disk space, the easiest way to save space is to not install the developer documentation. The developer documentation takes about 1 GB of disk space, and you can view all the documentation online anyway.

Writing Standard C++ Programs

The people most likely to ask if they can write C++ programs on Mac OS X are Mac-using college students who are taking a class on C++ programming. C++ programming classes normally teach standard C++, which will run on most operating systems, including Mac OS X.

To write a standard C++ program with Xcode, you must create a C++ Tool project. C++ Tool projects are used to write command-line programs without a GUI, which makes programming easier. You can read an article on the Xcode Tools Sensei page that contains detailed instructions on creating and building a C++ Tool project.

Writing C++ GUI Programs

After learning C++ you may want to use your newfound knowledge to write Mac OS X GUI programs. You have three options for writing Mac GUI applications in C++. First, you can use Cocoa, writing your user interface code in Objective C and the rest of your code in C++. You must give your Objective C files the extension .mm, which tells Xcode to treat them as Objective C++ files. Objective C++ lets you mix C++ and Objective C code in the same source file.

Second, you can use Carbon, which is Apple’s C API for writing GUI applications. If you choose to use Carbon, you may want to look into Nano, an open-source C++ framework built on top of Carbon.

Third, you can use a C++ cross-platform GUI framework like Qt or wxWidgets. I haven’t used either of these frameworks, but you should check them out if you are interested in supporting Linux and Windows as well as Mac OS X.

Comments (46) | Trackback

46 Responses to “Writing C++ Programs on Mac OS X”

  1. Matthew Riley says:

    thanks for this bit. I am a newbie so green the Great Gazoo would pale by comparison. Clour-wise.

    This is has been a great help in getting me started in the direction I want to go in.

  2. Anonymous says:

    Hey, thanks for the info, really helpful stuff :-)

    Answered all my questions in one fell swoop.

  3. Anonymous says:

    apple was and is technically more skillful than microsoft, but failed in two things:

    1. didn’t make itself more open

    2. didn’t provide sufficient compilers & IDE to attract people to develop software for it

    see, i tried very hard but got totally frustrated on http://www.apple.com, i even couldn’t find a link for code development. all talking about on the web are end-user products such like games, games, and games.

    therefore, today apple is still a child play.

    i’m sick tired of microsoft, but apple is still a child who has no idea of how to do a business

    by kenn

  4. Mark says:

    Kenn,

    Apple’s developer site is at the following URL:

    http://developer.apple.com

    Every copy of Mac OS X ships with the Xcode Tools so anybody running Mac OS X has access to an IDE to develop Mac software.

  5. Anonymous says:

    Problem with steve jobs is he thinks only money, is selfish, is arrogant and doesn’t know how to share the wealth and knowledge. He is afraid if he exposes apple to the outer world like windows its definitely gonna crash like hell.

  6. Anonymous says:

    I was wondering how I can run the C programs that I write using Xcode. I am new to C and I started on PC so I don’t know what to do.

    THANKS

  7. Mark says:

    To Anonymous who wanted to know how to run his C programs,

    To run your program, choose Build > Build and Run in Xcode. Doing so will compile your C code into an application and run it in Xcode. If you're using Xcode 3, choose Run > Console to see the output from the program.

    After building your program, you can also run it by double-clicking on it in the Finder. The Terminal application will launch, then your program will run.

    If you have any more questions about C programs, please email me instead of posting comments here. I have a link to my site under Links on the right side of the blog page. I moved my blog and the post is over 18 months old so I can help you more by email.

    NOTE

    If anyone reading this has any questions or comments about the material in the original post, which was about writing C++ programs on Mac OS X, feel free to comment here. Your comments could help someone who comes upon this blog post.

  8. Anonymous says:

    Thank you so much!!!

  9. Anonymous says:

    Hello Mark,
    C++ beginner here wanting to learn how to build a simple application with gui on my Intel mac running Leopard. I got xcode 3 running but sadly don’t know much about it. After fruitless searching on the internet, who or what or where can go to help with this endeavor? Is there any online class that teaches xcode 3? Is there someone I can pay to answer questions or teach one on one? How do c++ programmers who want to develop on the mac learn all this stuff?
    I bought the xcode2 book but got stuck because all the NIB stuff is different. Bought xcode3 unleashed book and there is no c++ in it.
    Any suggestions would be appreciated.
    Thanks.

  10. Mark says:

    To Anonymous August 18,

    If you want to write GUI programs for Mac OS X, learn Cocoa. Learning Objective C with your C++ knowledge will take much less time than learning Carbon. You can also write Cocoa programs in Python and Ruby if you hate Objective C that much.

    Learning Carbon is going to be very difficult if you’ve done no Mac programming before. There are no Carbon books to buy, which means you have to rely on Apple’s Carbon documentation. If you post a Carbon question on a Mac programming board, people will tell you to use Cocoa. More Mac developers use Cocoa, and the people that use Carbon most likely have been doing Mac programming for a long time, which means you’re not going to find a lot of blogs and articles about beginning Carbon programming. Plus, Carbon is not going to be ported to 64-bit, which means Carbon’s days are numbered.

    If you’re determined to write Mac GUI programs in C++, use a cross-platform framework. Otherwise, use Cocoa.

  11. mayank says:

    hi mark

    i was trying to use Xcode for writing a C++ code.

    It was my first time writing C++ on Xcode so i was trying with a basic check to find out how to compile and run ite.

    I went to XCode and chose the C++ file option. In the opened window i wrote the code. The problem is when i try to compile the code. I used build and run option but it does not do anything.

    can u help me how to compile the code and check the errors in my code.

    I m not writing any GUI or other software but trying to improve my knowledge of C++ coding.

    I hope you help me. I know it is silly but i m a newbie.

    Thanks in advance. I am writing in the blog as I could not find your email anywhere.

  12. Mark says:

    You have to create a project in Xcode. Every program you write in Xcode, no matter how small, requires a project. If you just create a C++ file, you won’t be able to compile the file. You need a project to compile the code.

    People who are using Xcode to learn C++ should create a C++ Tool project. If you want to be led through the creation of a small C++ program in Xcode, read the Writing Standard C++ Programs section of this blog entry. There is a link to my Xcode book page. On that page is an article introducing Xcode. Read that article.

    For those of you having trouble finding my email address, there is a link to my site (Me and Mark Publishing) under Links on the right side of the page. Once you get to my site, click the Contact link on the site map. On the contact page is a link you can use to send me an email.

  13. Quentin says:

    Thanks so much for the help guys. I’m a music engineer hoping to get into game audio in the future. I’m interested in this C++ knowledge and it’ll probably be beneficial to me.

    Peace.

  14. Anonymous says:

    Mark,
    I simply want to compile a c++ program from the MAC terminal. I am in the same directory as my program but when I type:
    g++ -o Sampleprogram.cpp sample2.cpp to compile I get:
    -bash: g++: command not found
    I’ve already downloaded Developer Tools, what am I missing?

    John

  15. Mark says:

    John,

    Assuming you installed the developer tools, I don’t know why you would get the error message you did. If you were in the wrong directory or misspelled a file name, you would get an error message saying there was no such file. Go to usr/bin and make sure g++ is in that directory.

    One thing I noticed in the sample command you posted is you should change Sampleprogram.cpp to Sampleprogram. The file that appears after the -o flag is the name of the executable file, and you don’t want the executable file to have a .cpp extension. When you slap the .cpp extension on an executable file, the Finder treats it as a source code file instead of an executable file. When you double-click the file in the Finder, the program won’t launch. Instead, the operating system opens the file in a text editor, and the file contains a bunch of indecipherable characters.

  16. Anonymous says:

    Mark,
    Thanks for replying so quickly. Her is the latest:

    john-crispins-computer-3:~ johncrispin$ which g++
    no g++ in /bin /sbin /usr/bin /usr/sbin
    john-crispins-computer-3:~ johncrispin$ g++ -o Sampleprogram sample2.cpp
    -bash: g++: command not found
    john-crispins-computer-3:~ johncrispin$ g++ -o sample2 sample2.cpp
    -bash: g++: command not found
    john-crispins-computer-3:~ johncrispin$

    When I go to the Linux environment on my school’s server this all works fine, but it should work on the Mac. Any ideas?

  17. Mark says:

    It sounds like g++ is not installed on your Mac. To verify this, create a C++ Tool project in Xcode and see if you can build it. I doubt you’ll be able to build it.

    I recommend uninstalling the Xcode Tools and reinstalling them. The uninstall script is in the Library folder inside the folder where you installed Xcode (/Developer by default). You’ll have to run the script inside the Terminal application.

  18. Anonymous says:

    I want to run a basic C++ program code on my mac. Does anyone know how to start one up… i’ve downloaded a Cpp edit program, just dont know how to set it up? Can anyone help, this is for a class i’m taking… i guess im the only one who owns a mac.

  19. Mark says:

    If you’ve compiled your command-line program, double-clicking the executable in the Finder will run the program.

    To write and compile your code, you have two options. Option 1 is to use Xcode. I have a link in the blog post that will lead you to an article that walks you through creating a command-line C++ program in Xcode.

    Option 2 is to compile from the command line. Write your code in a text editor. Launch the Terminal application. Navigate to the directory where your code is. Run gcc. The comment on this post on August 31,2008 at 11:46 PM Eastern Time shows how you would run gcc.

  20. Anonymous says:

    Thanks Mark… I’m planning to use Xcode.. and I checked out that link… im just still not sure if i found the article that shows you how to start a basic C++ program.

  21. Mark says:

    The article that walks you through the creation of the C++ program is the article introducing Xcode in the Free Stuff section.

  22. polycat33 says:

    Hi, I was hoping for more information about installing XCode… you say:

    “The easy install of the Xcode Tools is over 1.5 GB, which is overkill if all you want to do is learn C++. You can perform a custom install of the Xcode Tools to reduce the size of the install.”

    All I want to do is learn C++, so what part of it do I need? I opened it with Pacifist but there is SO much stuff inside, I have no idea what I need out of it. If you could, can you provide details on what bits need to be installed to JUST do C++ stuff? Thanks!

  23. Mark says:

    Polycat33,

    You’re replying to a post that is almost two years old. Things have changed a bit since I wrote the original post.

    If you’re using Xcode 3, you need to install the Developer Tools Essentials package. Unfortunately, the Xcode installer doesn’t let you customize what parts of the Essentials package you want to install so you get stuck with a 2-3 GB install. If you have 3 GB of disk space to spare, installing the Essentials package is the easiest way to go.

    If you’re running Leopard and low on disk space, I would recommend signing up for a free ADC account and downloading Xcode 2.5. In this case, you’ll also need to install the Developer Tools Essentials package, but it takes 550 MB of disk space instead of 2-3 GB.

  24. polycat33 says:

    Using the installer there aren’t a lot of options for installing a small amount of the package, however using Pacifist allows one to look at the contents of an installer package and pick out only certain pieces. I was hoping to do this, if I could identify which pieces are necessary for just C++. I’m not running low on disk space, I just really don’t want to install 3GB worth of stuff just to do something that should be very basic. Any assistance is much appreciated, thanks!

  25. Mark says:

    I can’t give you an exact answer. I’ve never tried to do a minimum install. If I were in your situation, I would install the following packages from the Xcode 3.1 package:

    1. One of the GCC packages. I would go with GCC 4.0.
    2. The Xcode.xExtras packages.
    3. The DeveloperDiskImage package.
    4. The DeveloperTools package.
    5. The DeveloperToolsCLI package.
    6. The DeveloperToolsSystemSupport package.
    7. The DevSDK package.

    I have not verified that this works, but installing those seven packages should let you do C++ development without a lot of waste. The biggest space eaters in the Xcode installation are the following items:

    Developer documentation.
    The Mac OS SDKs. Install just one, not all three.
    The Java SDKs.
    Developer examples.

  26. Anonymous says:

    There's no need to download Xcode if you want to make C++ command-line tools. What you do is write C++ code into a .cpp file just using macs built in text editor. It is essential that you set it to plain text mode. To do this go into the preferences>'new document' tab>set the radio button to 'plain text'.

    We create file for example "example.cpp". Then open the terminal (applications>utilities>terminal) and in the terminal you type g++ example.cpp -o example. The first word calls on the GNU C++ compiler, the second word is the source file, the third word calls a parameter to set the name of the outputted executable and the 4th word is the value of that parameter. To run the executable, type in the terminal ./example

    Note: If your source files are saved in a path other than your home folder, you need to specify the path in the terminal. Eg, if i saved example.cpp a folder called ‘programmingStuff’ on my root drive. In the terminal I’d specify the path in the compile command like so: g++ /programmingStuff/example.cpp -o example

    This saves you the download and compiles quick. The G++ compiler is an extension of the GCC compiler, but instead of C, this does C++. If you want to compile .C files in the terminal, you merely just have to replace the “g++” with “gcc”. That’s all there is to it.

  27. Anonymous says:

    Hi. I was finally able to create a cpp file, compile and execute, but it was through the command line (using terminal and g++). I tried building using Xcode but I kept getting an error about SDK, unknown path. When I try “Set Active SDK” nothing shows up to set it to (no options). I’ve reinstalled DevSDK several times with no luck. But at least I do have this working using other method. Does anyone know how to get options to show up when I select “Set Active SDK”? Thanks in advance!

  28. Mark says:

    To the person with the SDK problem,

    I recommend uninstalling Xcode, then reinstalling it. A clean reinstall of Xcode should place the Mac OS X SDKs in the proper location so your C++ Tool projects build properly in Xcode.

  29. Anonymous says:

    I’ll try that, thanks!

  30. Anonymous says:

    Hell, I just tried to test my C++ programs on mac leopard Xcode 3.o. But I found that the font size was too small in Xcode IDE. Is there anyway to enlarge the font size? I wil be grateful if someone can let me know how to adjust font sizes. Thanks.

  31. Mark says:

    To adjust the font size, open Xcode's preferences by choosing Xcode > Preferences. It sounds like you want the font size larger in the debugger console. To adjust the font size in the debugger console, click the Debugging button in the preference window's toolbar.

    If you want to adjust the font size of your source code in Xcode's editor, click the Fonts and Colors button in the preference window's toolbar.

  32. Anonymous says:

    Hey, thanks a lot for your instructions. Now I know how to enlarge font sizes in Xcode’s code editor. But can you please tell me how to enlarge font sizes on side bars? Thanks in advance.

  33. Mark says:

    I’m not sure what you mean by side bars, but the following email thread from Apple’s Xcode mailing list could help you:

    http://www.cocoabuilder.com/archive/message/xcode/2007/11/7/16928

  34. Anonymous says:

    Thanks for your information again.

    But this time the hyperlink does not lead to a solution for the font-size problem I face. I still cannot enlarge font sizes of the left of the Xcode IDE.

  35. Mark says:

    If setting PBXTableFontSize doesn’t increase the font size, I would suggest asking on Apple’s Xcode mailing list.

  36. Anonymous says:

    Hi, Mark. Great page. Great links. I am completely new to XCode but I managed to get a little C++ project going in no time. I have a question, though. I am rebuilding from scratch a more complex program which I had running in python, with a GUI built with Tkinter, a python module using Tcl/Tk. Python turned out to be too slow for my needs, so I am migrating to C++.
    Now, the question: would I be able to use C++ for the engine bit of the program and python’s Tkinter for the GUI, and combine everything into Xcode? I mean, would my C++ core talk to the python interface through Xcode’s framework? Or should I rather drop python’s Tkinter and use Cocoa istead? ( I know Tkinter, I am a complete novice in Objective C)
    My initial plan, before I got my new Mac Book, was to have C++ core talk to a python interface via sockets ( or files on the hdd). Would the XCode make my life easier?

    Thanks again,

    Gabriel

  37. Mark says:

    Gabriel,

    Yes, you can use C++ for the non-GUI portions of your program and use Tkinter or Cocoa for the GUI.

    The decision of which GUI framework to use depends on the program and who is going to use the program. If your program is intended for the mass market of general Mac users, going with Cocoa would be a good choice. You can write Cocoa applications with Python using PyObjC. Xcode 3 has project templates for Cocoa Python programs. If the program has a more specialized audience, you would be better off sticking with Tkinter.

    I’m not familiar with Tkinter so I don’t know how easy it is to integrate with Xcode. I know Mac OS X ships with the Python, Tcl, and Tk frameworks. If there’s a Tkinter framework available for Mac OS X, using Xcode will be easy. All you would have to do is add the framework. If there isn’t a framework available, more work would be required.

    If you need a more detailed answer, send me an email. There’s a link to my website in the sidebar on the right side of this blog.

  38. Robin Solanki says:

    Thanks I really needed this.

  39. Alan says:

    I am also a programming newbie. I managed, using a source or two, to piece together a program that should prompt you for X, then add that to 2, and it retrieves your result.

    Only problem(s)?

    When I try to go to the "run" or "go" section of Xcode (or try to compile for that matter), it opens a window that says "Stop Executable". The program still works if I open it through terminal (which I believe is the standard command line interface for mac.

    For curiosity sake, what is X11 and how do you create graphics for c++ programs on a mac? (My cousin created a small single player game with graphics).

    Thank you for your time.

  40. Mark says:

    Alan,

    If you're using Xcode 3, choose Run > Console to enter input and see the output from the program.

    X11 is the windowing system Unix operating systems use. Mac OS X has an X11 application that lets you run X11 apps on your Mac. X11 should be in your Applications folder under Utilities. If it's not there, you'll have to install it from your Mac OS X DVD.

    You would use OpenGL to write graphics programs on a Mac. You would use an image editing program like Photoshop or GIMP to create the graphic images.

  41. poon says:

    Hihi, thanks for the info ^^.

    I've been using SAMS Teach Yourself C++ Fifth Edition by Jesse Liberty and Bradley Jones to learn C++, but I'm having some problems with function declarations and definitions on Xcode. I follow the code exactly, but I get errors. I tried multiple examples from the book, and all of the programs regarding functions produce errors. Here's an example:

    // Listing 5.2
    // Demo of the Use of Local Variables and Parameters

    #include iostream

    float Convert(float)
    int main() // error: expected initializer before "int"
    {
    using namespace std;

    float TempFer;
    float TempCel;

    cout << "\nPlease enter the temperature in Fahrenheit: ";
    cin >> TempFer;
    TempCel = Convert(TempFer);
    cout << "\nHere is the temperature in Celsius: ";
    cout << TempCel << endl;
    return 0;
    }

    float Convert(TempFer) // error: 'TempFer' was not declared in this scope
    { // error: expected ',' or ';' before '{' token
    float TempCel;
    TempCel = ((TempFer – 32) * 5) / 9;
    return TempCel;
    }

    For the first error, when I take out "float Convert(float)", that error disappears. So does "#include iostream" have to immediately precede "int main()"?

    Thank you very much!

    ^w^

    PS> My tags are taken out because HTML cannot accept it.

  42. Mark says:

    Poon,

    Read the tutorial on functions at the following URL:

    http://www.cplusplus.com/doc/tutorial/functions

    If you read the tutorial, you'll see that putting the code for your Convert() function before main() should eliminate the compiler error.

  43. poon says:

    Hihi Mark,

    Thanks, that solved it!

    ^w^

  44. Eddie Lopez says:

    Thanks for posting this blog!! It was a great help.

  45. 1 says:

    Any help greatly appreciated – I have just built the OpenCV framework (got headers and dynamic libraries) for doing pattern recogntion – but cant get it to work with Xcode (3.2). Get the following errors:

    ———————————–
    Undefined symbols:
    "_cvLoadImage", referenced from:
    _main in main.o
    "_cvNamedWindow", referenced from:
    _main in main.o
    "_cvReleaseImage", referenced from:
    _main in main.o
    "_cvShowImage", referenced from:
    _main in main.o
    "_cvDestroyWindow", referenced from:
    _main in main.o
    "_cvWaitKey", referenced from:
    _main in main.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    ———————————–

    Does anyone know what these mean? 1. Max.

Powered by WordPress