C++ in XCode

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

  1. airkarol macrumors 6502

    Joined:
    Nov 12, 2005
    #1
    Hi,

    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,

    David
     
  2. Nermal Moderator

    Nermal

    Staff Member

    Joined:
    Dec 7, 2002
    Location:
    New Zealand
    #2
    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

    plinden

    Joined:
    Apr 8, 2004
    #3
    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

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #4
    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

    Joined:
    Sep 19, 2009
    #5
    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

    Joined:
    Nov 12, 2005
    #6
    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

    kainjow

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

    Joined:
    Mar 8, 2004
    #8
    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