Creating your first project with Xcode

These notes will take you through the process of creating your first C++ program in Xcode.

The source code files you will need to create to write a C++ program in Xcode are organized into projects. Here are the steps you will follow to create and run your first project in Xcode.

  1. When you start up Xcode you will see the Xcode welcome screen. From this screen you can either open an existing project or create a new project.
  2. Click the button to create a new Xcode project. This brings up the new project window.
  3. Select the Application project type from the list on the left. Select 'Command Line Tool' in the top-right pane and click Next. On the next screen type a name for your project and make sure that 'C++' is selected as the language for the project. Click the Next button and then click Create to save your new project to a project folder.
  4. Once you have created the project you should see a window something like this. This window is the main window for Xcode.
  5. The pane on the left side shows you the files that make up your project. Most of the programs we will write for the first few weeks of this course will consist of a single C++ source code file, usually named 'main.cpp'.
  6. The pane in the middle is the source code editor pane. This is where you will type the source code for your program. For our first example you won't need to type anything - just copy and paste the code you see in the source code section below to replace the source code that Xcode generated for you when you set up the project.
  7. Since our first example program needs to work with a couple of text files we will need to create those and add them to our project. To add a text file to the project, right-click on the project folder in the project view in the left pane and select the option New Item... In the dialog that appears, select the Other category and the Empty file option. Name the file numbers.txt.
  8. Type some numbers into the numbers.txt file and then repeat the process to create an empty text file named "copy.txt".
  9. When your program runs it will need to locate these text files. The best way to make this work is to replace the simple file names "numbers.txt" and "copy.txt" in the open() commands with full paths to the files. You can find the full path information by clicking on a text file in the project pane. When you do that, additional information (including the full path to that file) will appear in the pane on the right.
  10. To compile and run your program, click the run button in the top left corner of the window.
  11. When your program runs, XCode will open an output pane. You can see any output that your program generated in that pane.

Source Code

Below is the source code for our first example program. Copy and paste this code into the source code editor pane in Xcode.

#include <iostream>
#include <fstream>

int main(int argc, const char * argv[]) {
    
    std::ifstream in;
    in.open("numbers.txt");
    
    std::ofstream out;
    out.open("copy.txt");
    
    int x;
    while(in >> x) {
        out << x << std::endl;
    }
    
    in.close();
    out.close();
    
    std::cout << "Done!\n";
    
    return 0;
}