Creating a Command Line Application with Xcode

November 29th, 2013

Filed under: Xcode | 6 comments

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.


6 thoughts on “Creating a Command Line Application with Xcode

  1. […] I’ve started to learn c++. With xcode this is really fun. And if you have a lot of ebooks, then this is more then fun. Well, of course the first project is about to be command line app. Then came a problem with exporting it into executable app. How to do this in xcode? After some searching I found this link, which was pretty helpful, maybe it will help someone : creating executable command line tool […]

  2. Graham Hinton says:

    Yes, that’s the easy bit. Now how do you debug a CLI application with all the arguments and environment variables in requires to run properly? (Xcode 6)

    • Mark Szymczyk says:

      Graham,

      To debug your application, set a breakpoint. The easiest way to do this is to click in the gutter to the left of a line of code. When you run the project, Xcode will stop at the line where you set the breakpoint. The debug area, which is pretty much blank in the screenshot in the Running the Project section of the article, will show your variables and let you step through your code.

      Use the scheme editor to set arguments and environment variables. You can access the scheme editor from the project window toolbar. It’s next to the Play and Stop buttons. If you search for command line arguments in the blog’s search field, you’ll find a post where I show you how to set command-line arguments and environment variables. Searching for scheme editor will also bring up an article that shows how to set the working directory for command-line applications.

      If you need more information on debugging and the scheme editor, I have the electronic version of my Xcode book available to download. Click the Books section at the top of the site. The book was written for Xcode 4, but much of it still applies to Xcode 6. My blog has tags for Xcode 5 and 6 that cover changes in the book material for Xcode 5 and 6.

  3. Ethan Brooks says:

    Hi. Is there a way to rename main.c? I’m trying to import an existing source which has a different name for the file with main() in it.

    • Mark Szymczyk says:

      Ethan,

      Select the main.c file in the project navigator and press the Return key. Doing this will let you rename the file.

Leave a Reply

Your email address will not be published. Required fields are marked *