C++ in XCode

Discussion in 'Mac Programming' started by airkarol, Mar 17, 2010.

  1. airkarol macrumors 6502

    Nov 12, 2005

    I am taking a C++ course at my university. A lot of assignments require me to make a single .cpp file with a given name. I haven't been able to get them to run unless part of a project in XCode, with the name main.cpp.

    How can I run / compile single .cpp files using XCode Tools, or is there a better compiler to use? The school recommends Bloodshed Dev C++, but that is not available for OS X.

    Also, I have to print out screenshots of what would normally be seen in the Command Prompt window on Windows. The only thing I've seen so far for showing I/O is the debugging console; however there is also a lot of useless information on there, such as the toolbar at the top, and information displayed while the program is running. Could it run in Terminal without all of that?

    Thanks in advance for the help,

  2. Nermal Moderator


    Staff Member

    Dec 7, 2002
    New Zealand
    It's been many years since I've done this, but I think that there is (or at least used to be) a project type called "C++ Tool". This makes a "clean" console app.
  3. plinden macrumors 68040


    Apr 8, 2004
    If it's just a single cpp file, don't even bother with XCode. Open Terminal, write your code, compile it with g++ and run from the command line. XCode uses gcc/g++ for compilation so using it doesn't gain you anything but syntax highlighting, that you can get using something like TextWrangler (others will have favourite editors they can tell you about)
  4. lloyddean macrumors 6502a

    May 10, 2009
    Des Moines, WA
    The name of the "main.cpp" file can be changed within an Xcode project by right-click and selecting "Rename" in the resulting popup-menu.

    If using Mac OS 10.6.x a user selectable area screen capture can be done using Command-Shift-4, selecting the area you wish to capture and pressing the mouse button. The captured image will be written to your Desktop.
  5. mac2x macrumors 65816

    Sep 19, 2009
    I use Xcode as a text editor, but I compile in Terminal. Do whatever would be most efficient for you. It certainly would make sense to consolidate everything into the command line.
  6. airkarol thread starter macrumors 6502

    Nov 12, 2005
    For some reason the entire window has to be in the screenshot, ie. the top bar of the program. When renaming the file in Xcode, I get an error having to do with the main.cpp file not being there, even though i defined a main function within the file.

    I've successfully compiled with g++; however, I haven't been able to figure out how to run the file.
  7. kainjow Moderator emeritus


    Jun 15, 2000
    If you used the -o argument to give it a name, run it like
    where name is what you passed to -o. If you didn't, run it like:
  8. Mac_Max macrumors 6502

    Mar 8, 2004
    I've found that the easiest way to program in class is to use the C++ console app and run it within XCode's console. The console is located under the Run menu.

Share This Page