Xcode C and C++ on Macintosh OS X

See if your Mac has the application Xcode. Go to the Finder, pull down the File menu, select Find, and search for Xcode.app. Its icon is a hammer on a blue background. If you don’t find Xcode, download it from here.

To install the newest version of Xcode, you might need the newest version of the Mac OS X operating system. If you don’t feel like upgrading your operating system, get an old version of Xcode from connect.apple.com.
Downloads → Developer Tools
and look for older versions of Xcode.

A one-file C++ program

  1. Launch the application Xcode.app.

  2. FileNewNew Project…
    In the left panel, under Mac OS X, select Application.
    In the upper right, select Command Line Tool.
    Next
    Product Name: Prog
    Company Identifier: edu.nyu.scps
    Type C++
    Next
    Save it on the Desktop and press Create.

  3. In the left panel (the Project Navigator), double-click on main.cpp to edit it. (If you don’t see main.cpp,
    View → Navigators → Show Project Navigator )
    Change or insert code where it says // insert code here….
    If you change the std::cout to cout, you must insert
    using namespace std;
    If you change
    return 0;
    to
    return EXIT_SUCCESS;
    you must insert
    #include <cstdlib> //for EXIT_SUCCESS

  4. When you are done editing, save the main.cpp file.
    FileSave

  5. Press the Run button in the upper left.
    You should see the output (“hello, World!”) in a panel at the bottom. If you don’t see it,
    View → Debug Area → Activate Console
    or
    View → Debug Area → Show Debug Area

  6. To run the executable file from the Terminal command line, we first have to find the executable file. In the left panel of Xcode, you should see the Products folder. (If you don’t see the Products folder,
    View → Navigators → Show Project Navigator )
    Open the Products folder, control-click on Prog, and select Show in Finder. Control-click on Prog in the Finder and select . The Info (Under General → Where) will tell you where to go to find the file Prog. Launch the Terminal application and type the following commands in it. The argument of the first cd should be the outrageously long directory name you just got from Get Info.
    cd /Users/nyuuser/Library/Developer/Xcode/DerivedData/Prog-ccyafydmrhjgzqetmkytdgfgfdek/Build/Products/Debug 
    pwd
    ls -l prog
    
    ./prog               (direct the standard output to the terminal window)
    echo $?              (see prog’s exit status)
    
    ./prog > prog.out    (direct the standard output (cout) to the file prog.out)
    ls -l prog.out
    
    ./prog 2> prog.err   (direct the standard error output (cerr) to the file prog.err)
    ls -l prog.out
    
    ./prog > prog.out 2> prog.err
    ./prog > prog.combined 2>&1
    

A C++ program that calls functions written in C
(Chapter 1, p. 86, Homework 1.7.3a)

  1. As above, create a “C++ Tool” project. This time, name it term.

  2. Double-click on main.cpp to edit it. Overwrite the entire file with the main.C that draws XX.
    FileSave

  3. Download the files term.h and term.c to your term folder. Do not rename them.
    ProjectAdd to Project…
    Add the files term.h and term.c to the project. You’ll have to press Add twice for each file.

  4. Double-click on term.c to edit it. Uncomment the #define UNIX. Change each expression in column 1 to the corresponding expression in column 2.
    stdscr->_begx       getbegx(stdscr)
    stdscr->_begy       getbegy(stdscr)
    stdscr->_maxx       getmaxx(stdscr)
    stdscr->_maxy     getmaxy(stdscr)
    size_t is another name for unsigned long on this platform. In term_puts, change length %u to length %lu and change subscript %u to subscript %lu.
    FileSave

  5. Add the dynamic library libcurses.dylib to your project.
    ProjectEdit Active Target "proj"GeneralLinked Libraries
    Press the plus sign and select libcurses.dylib. Press Add.

  6. Build and Run.

  7. You will have to run the executable in the Terminal application as in the above ¶ 7, not in the Xcode application.
    cd /Users/yourname/Desktop/term/build/Debug
    pwd
    ls -l term
    
    ./term
    Please type printable characters ending with a q.
    Hello, how are you? q
    echo $?
    0