Skip to main content

Creating a Command Line Application with Xcode

·3 mins

When learning a programming language, you generally start by writing command-line programs so you can focus on learning the language. If you’re learning C, C++, or Objective-C, you can use Xcode to write and run your programs. In this article you’ll learn how to setup Xcode to create command-line applications. Short answer: create a command-line tool project.

The instructions for this article are for Xcode 5, but they should also apply to Xcode 4 with minor changes. If you have an older version of Xcode, you can find an article that creates a command-line application in Xcode 2 and 3 in the Articles section of my website in the section Xcode Articles.

Create the Project #

Every program you create in Xcode requires a project, even a program with only one source file. So let’s start by creating a new project. In Xcode choose File > New > Project. The New Project Assistant opens. On the left side of the assistant is a list of project categories. Select Application in the OS X section to see a list of Mac application project templates. Select the Command Line Tool template, as you can see in the following screenshot:

Command Line Tool Project Step 1

Click the Next button to move to the next step.

Command LIne Tool Project Step 2

Enter the name of the project in the Product Name text field. The name of the project is also the name of the program you’re creating. Enter an organization name in the Organization Name text field. You can enter your name if you are not part of an organization. You don’t have to worry about the company identifier for a command-line program, but the company identifier generally takes the following form:

com.CompanyName

Use the Type menu to choose the language. I chose C++, but you can also choose C or Objective-C. Choose Foundation to create a command-line Objective-C program.

Click the Next button to move to the last step, which is to choose a location to save your project. Click the Create button to create the project. At the bottom of the Save panel, you should see an option to create a local git repository for your project.

Create Git Repo

You don’t need to create a git repository for this project so you can deselect the Create git repository checkbox.

Editing Source Code #

Now that you’ve created the project, the next step is to start editing source code. On the left side of the project window is the project navigator, which lists all the files in your project.

Project Navigator

Select the main file to open it in Xcode’s file. In the screenshot the main file is named main.cpp because I created a C++ project. If you chose a different language, your main file will have a different file extension. You should see the following code in the main file:

int main(int argc, const char * argv[]){
    // insert code here...   
    std::cout << "Hello, World!\n";
    return 0;
}

If you have a C or Objective-C project, the code will look a little different, but the code does the same thing: display the message Hello, World! in the console. Let’s change the message. Change the following string:

"Hello, World!\n"

To the following:

“This is my first Xcode project.\n"

Running the Project #

Click the Play button at the top of the project window to build and run your project. The output of the program should appear in the debug console at the bottom of the project window.

Debug Console

If you don’t see the debug console, choose View > Debug Area > Activate Console to show the console.

Where’s the Application? #

Click the disclosure triangle next to the Products folder in the project navigator to show the application you built. Select the application, right-click, and choose Show in Finder. Doing this will show your application in the Finder so you can run the application outside of Xcode.