Build a Mac App From Terminal

Matthew Campbell, November 28, 2012

Creating a new Mac app is as easy as opening up a text editor, typing in some Objective-C code and using the Terminal command line to compile your app. This procedure works best if you have installed Xcode from the Apple App Store already.

Here’s how you can build a simple app from Terminal.

Install Xcode

Even though you don’t need to use Xcode much for this tip, you will need the compilers that come with Xcode to build your app from Terminal. Of course, if you have any intention at all of developing iOS or Mac apps then you will need Xcode so you may as well download this tool from the Apple App store now.

Click here to download and install Xcode from your Mac: Install Xcode.

BTW: this is a free tool to use on your Mac. To distribute Mac or Mac apps on the app store you’ll need to pay a yearly fee to Apple ($99 for each).

Install Command Line Tools

Starting with Xcode 4.3, the command line tools that we’ll be using are not installed by default. So, you’ll have to do this yourself. Open up Xcode from your Applications folder (or you can use Spotlight). Go to Xcode > Preferences > Downloads . Make sure the Components tab is chosen.

Click the Install button next to the Command Line Tools item to install the tools we’ll be using. Once the installation is complete you may quit Xcode.

Write a Mac Program

Use your favorite text editor to write the program that we will eventually use to make the Mac app. I always use TextMate for text editing, however the built-in Mac editor TextEdit is fine as long as you remember to save your file as plain text.

Create your file on your desktop and name it macapp.m. Obviously, the location and name are totally optional, but remember to use something that will be easy to remember when you’re using the Terminal command line.

Add the following code to your source code file:

#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]){
 	@autoreleasepool {
		NSLog(@"%@", @"Hello World");
	}
	return 0;
}

This code resembles what you typically see in a simple C program along with some Objective-C code added in (the @autorelease, NSLog and Foundation import are all Objective-C).

What this program will eventually do is write the Hello World message to the console window.

Use Terminal to Navigate to the Mac Program

Open up Terminal on your Mac for this step. If you can’t find Terminal use the Spotlight feature to locate this program. You can get to the Spotlight feature by clicking on the magnifying glass in the top right hand area on your Mac screen.

Once you have Terminal opened on your desktop you will need to navigate to your saved source code file. Use the Terminal command [folder name] to move to a particular folder on your Mac. Use the Terminal command ls to see the contents of the current directory that you’re in.

For example, when I used Terminal today I had to do this to get to my Mac Desktop.

cd MattjDrake
cd Desktop

Often to verify that I’m in the right place I’ll use the ls command to make sure I’m in the same directory that my file is located in. So, today I did this:

ls

and when I hit return I got a listing of the files on my desktop:

$RECYCLE.BIN			Untitled.html
001-001-App-Options-Dialog.png	macapp.m
001-001-Empty-App-Files.png

At any rate, if you’re familiar with Unix style commands then this all looks familiar and you can do many more things from the command line. For you Windows users, this is like the DOS command line except that some of the commands will be a bit different and the file system will be different from what you’re used to.

Compile the Mac Program

Once you have Terminal pointed to the right folder you can type this into the Terminal command line to compile your Mac app.

clang -fobjc-arc -framework Foundation macapp.m -o macapp

If the compilation was successful, you will receive no notification. However, you may test your app by typing the command open macapp and pressing return. You will see another Terminal window pop up with something like this:

Last login: Wed Nov 28 15:26:46
You have mail.
/Users/MattjDrake/Desktop/macapp ; exit;
new-host-3:~ MattjDrake$ /Users/MattjDrake/Desktop/macapp ; exit;
2012-11-28 15:42:24.687 macapp[15493:707] Hello World 
logout

[Process completed]

I highlighted the Hello World text so that you can see it.

So, that’s it boys and girls – you now know how to set up your own Mac application using just the command line tools. Of course, I glossed over many nuances about Objective-C, Mac and iOS apps here. Feel free to take a look around the rest of the website to learn more about the tools and technology that you can use to make your own apps for Mac and iOS.

Also – leave me some comments below if you have more to add or if there are any questions.