Build an iPhone app from the command line

If you, like me, are an old-time Unix command-line fanatic now doing iOS development, you've probably wondered if you can build an iPhone app from scratch, entirely outside of XCode. After all, Mac OS/X is a *nix, and all the familiar tools — Make, cc, ld — are all there. So, can you build and compile completely outside of XCode? As it turns out, yes, you can, and there are actually some speed advantages to doing so.

Now, before I continue — if you're looking to incorporate an automated build system like Cruise Control or Hudson into your development process in support of a development effort that normally works with XCode, you're probably far better off looking into the command-line tool xcodebuild that ships as part of XCode. However, if you just want to throw together a POC really quick, and you know your Objective-C syntax well enough that you don't really need the overhead of the XCode IDE (which can be quite a bit, in spite of Apple's impressive efforts to keep it manageable), you can compile and simulate an iPhone app using only command-line tools like we used to back in the day when men were men, women were women, and a compiler and a text editor was all we needed.

XCode is pretty good about telling you what it's doing for you behind the scenes, as long as you know where to look. For example, you can see all of the build parameters that were run as part of a build by opening up the Log Navigator (Command+7) and clicking one of the "build" logs as shown in figure 1.

Figure 1: Viewing the build logs.

As you can see, there's a lot of stuff going on in there, and as it turns out, it's not all stuff you necessarily need.

The smallest iPhone app I can think of that (arguably) does something "useful" is shown in listing 1 below.

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface MyDelegate : UIResponder< UIApplicationDelegate >
@end

@implementation MyDelegate
- ( BOOL ) application: ( UIApplication * ) application
           didFinishLaunchingWithOptions: ( NSDictionary * ) launchOptions {
  UIWindow *window = [ [ [ UIWindow alloc ] initWithFrame: 
    [ [ UIScreen mainScreen ] bounds ] ] autorelease ];
  window.backgroundColor = [ UIColor whiteColor ];
  UILabel *label = [ [ UILabel alloc ] init ];
  label.text = @"Hello, World!";
  label.center = CGPointMake( 100, 100 );
  [ label sizeToFit ];
  [ window addSubview: label ];
  [ window makeKeyAndVisible ];
  [ label release ];

  return YES;
}
@end

int main( int argc, char *argv[ ] )
{
  UIApplicationMain( argc, argv, nil, NSStringFromClass( [ MyDelegate class ] ) );
}

Listing 1: window.m, A useful(?) iPhone app

This "app" opens a window and displays "Hello, World" as illustrated in Figure 2 (it also leaks its UIWindow instance because I didn't create an autorelease pool. I hope your computer has enough memory to compensate for this).

Figure 2: Hello, World

If you save this as, say window.m, you can compile and launch it (in the simulator) without ever starting up XCode.

You must have XCode installed — the simulator isn't distributed independently of XCode and even the cross-compile libraries (which I'll detail further below) are part of the XCode bundle. However, you don't have to have it running to compile and test an app.

LLVM, or Low Level Virtual Machine, is Apple's preferred Objective-C compiler these days. If you've been doing iOS programming for a while, you probably know that it used to all be done in gcc (and can still be done that way, if you're so inclined), but the default is LLVM (clang on the command line), so that's what I'll use here. clang is invoked from the command line just like gcc — you give it an input file and an output file. I'll go ahead and create a Makefile for this process; the first iteration is shown in listing 2.

window: window.m
  clang -o window window.m

Listing 2: Rough cut at a makefile

This fails (and honestly, you probably expected that) with:

clang -o window window.m
window.m:1:9: fatal error: 'UIKit/UIKit.h' file not found
#import 
        ^
1 error generated.
make: *** [window] Error 1

If you have any experience with C programming, you can immediately see the problem here; I didn't give the compiler a -I flag pointing to this header file. Interestingly, though, this minimal makefile didn't error on my import of <Foundation/Foundation.h> on line 1 — yet I didn't tell it where to find this include file. In fact, it would have compiled without complaining if I had given it an input that didn't require UIKit. For instance, listing 3 will compile just fine.

#import <Foundation/NSString.h>

int main( int argc, char *argv[ ] )
{
    NSString *s1 = @"abc";
    NSString *s2 = @"123";

    NSLog( @"%@", [ s1 stringByAppendingString: s2 ] );
}

Listing 3: minimal.m, non-GUI objective-C class

minimal.m won't link, however, because although the header <Foundation/NSString.h> is found, the Foundation library itself isn't. To include it, you have to specify the framework:

clang -o minimal minimal.m -framework Foundation

From a coding perspective, -framework works a lot like the -l flag in gcc. But — where did clang find NSString.h? It's not in /usr/include, or /usr/local/include — in fact, you'll search in vain for a file anywhere on your system named "NSString.h" that's contained in a directory named "Foundation". However, if you do look for "NSString.h", you'll find a copy in /System/Library/Frameworks/Foundation.framework/Headers. But, by the strict "rules" of C compilation, that can't be where clang resolved it, because it's not in a directory named "Foundation". This is another Objective-C extension to clang. In addition to regular header include searches (like /usr/include), there's the notion of Framework searches. By default, clang will look under /System/Library/Framework for frameworks to include. Any such framework, if it has a Headers directory, will be resolved as Framework Name/header file. An ordinary iOS developer would not need to know or care about such low-level details, but when you're working so close to the system core, it does matter.

So what are these "frameworks"? Since NeXT's NeXTSTEP operating system and it's derivatives (Mac OS/X and GNUStep) are the only users of Objective C, it's sort of difficult to tease apart which parts of clang are "pure" Objective C and which parts are Mac OS/X extensions. I believe, however, that this concept of a "framework" is a native part of objective C. Essentially, a framework is a bundling of libraries and their headers and associated resources with built-in support for versioning. Anybody who's spent much time compiling and maintaining C-based packages on a Unix system can see how useful this is; the Linux community has been trying to standardize on something like this with pkginfo and RPM's for quite a while. clang has a -l flag that will link ordinary C libraries, but you'll likely never use this for iOS development.

So, what about UIKit? Well, it's not there under /System/Library/Frameworks. That's because it's not part of Mac OS/X; it's part of iOS. So how can you link to it? This is where the simulator starts to come into play. XCode installs the iPhone simulator and a set of cross-compile libraries for you to build against. XCode has a tendency to move things around from one version to the next, so you may have to hunt around to find the "core" directory, but as of XCode 4.6.1, the cross-compile libraries are found under:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/

Underneath this directory is another /System/Library/Framework, this time with a set of the Frameworks that iOS includes. (In fact, this directory serves as the root directory of an entire iOS "installation"). So, to get clang to compile an app that depends on UIKit, you have to add this directory to the framework search directory. You do this via the OS/X-specific "-F" flag to clang. So, the updated makefile looks like listing 4:

XCODE_BASE=/Applications/Xcode.app/Contents
SIMULATOR_BASE=$(XCODE_BASE)/Developer/Platforms/iPhoneSimulator.platform
FRAMEWORKS=$(SIMULATOR_BASE)/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/

window: window.m
  clang -F$(FRAMEWORKS) -o window window.m -framework Foundation -framework UIKit

Listing 4: First attempt at a cross-compile to iOS

The results are a little better, but this fails with:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/
iPhoneSimulator6.1.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFBase.h:146:14: fatal error: 
'MacTypes.h' file not found
#include 
         ^
1 error generated.
make: *** [window] Error 1
UIKit has a dependency on a framework called Core. This framework is part of Mac OS/X, so clang finds it and tries to use it, but UIKit relies on a different version of Core than the one that Mac OS/X uses. The problem here is that clang is trying to resolve its frameworks from the simulator directory, but it's still finding headers under /usr/include. The Unix C programmer's solution, of course, is to add a "-I" as shown in listing 5.

XCODE_BASE=/Applications/Xcode.app/Contents
SIMULATOR_BASE=$(XCODE_BASE)/Developer/Platforms/iPhoneSimulator.platform
FRAMEWORKS=$(SIMULATOR_BASE)/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/
INCLUDES=$(SIMULATOR_BASE)/Developer/SDKs/iPhoneSimulator6.1.sdk/usr/include

window: window.m
  clang 
    -I$(INCLUDES) \
    -F$(FRAMEWORKS) \
    -o window window.m -framework Foundation -framework UIKit

Listing 5: Second attempt at a cross-compile to iOS

This gets us further, but the compiler now emits a lot of errors along the lines of:

     'UIScrollView' is unavailable: not available on Mac OS X
Again, clang is trying to generate a Mac OS/X executable when what we want is an iOS executable. The solution is to add:
-mios-simulator-version-min=6.1 \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk
In fact, with isysroot, you can get rid of the -F and -I flags; this replaces the "/" directory as it pertains to the compiler. Now, the compile completes, but the link step fails due to:
Undefined symbols for architecture x86_64:
This is because, although I've requested some cross-compile options, I still haven't actually asked for a cross-compile. I have to do that with:
-arch i386
Almost there. This fails for me with a handful of "Undefined symbols for architecture i386". The reason is because I'm running on a 64-bit machine; to work around this, I have to add
-fobjc-abi-version=2
And voila! I get the executable file window. The completed make file is shown in listing 6.

XCODE_BASE=/Applications/Xcode.app/Contents
SIMULATOR_BASE=$(XCODE_BASE)/Developer/Platforms/iPhoneSimulator.platform
FRAMEWORKS=$(SIMULATOR_BASE)/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/
INCLUDES=$(SIMULATOR_BASE)/Developer/SDKs/iPhoneSimulator6.1.sdk/usr/include

window: window.m
  clang 
    -arch i386 \
    -mios-simulator-version-min=6.1 \
    -fobjc-abi-version=2 \
    -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk \
    -o window window.m -framework Foundation -framework UIKit

Listing 6: Successful attempt at a cross-compile to iOS

These are the fewest command-line parameters you can pass into clang and still get a working iOS executable. Now, how about running it? If I try to invoke it directly from the command line, it predictably fails with:

dyld: Library not loaded: /System/Library/Frameworks/UIKit.framework/UIKit
  Referenced from: /Users/joshuadavies/devl/test/objc/./window
  Reason: image not found
Trace/BPT trap: 5
Which makes sense - I compiled something for iOS, but now I'm trying to run it on a OS/X. As you undoubtedly know, XCode includes an iPhone emulator, and you can invoke it from the command line and pass in an app to simulate with the -SimulateApplication parameter. You can invoke it from the command line like this:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone\ \
Simulator.app/Contents/MacOS/iPhone\ Simulator \ -SimulateApplication ./window
(Notice the "./" in front of the application name; if you omit this, the simulator won't find your executable).

So there you have it — without XCode running or open, I've compiled and tested a complete, working iOS app. Note that this whole process does not build an actual iPhone bundle. iPhone runs under an arm architecture, whereas this build, although it was a cross-compile, still runs on an intel processor. To build for an actual deployment to an iphone, you'd have to change the architecture to arm, and to get it to install on a real device, you'd also have to deal with code signing. Still, it's interesting and fun to see how far you can go without actually running XCode.

Add a comment:

Completely off-topic or spam comments will be removed at the discretion of the moderator.

You may preserve formatting (e.g. a code sample) by indenting with four spaces preceding the formatted line(s)

Name: Name is required
Email (will not be displayed publicly):
Comment:
Comment is required
Wei, 2014-03-26
Awesome! Beautiful work! Thank you!
AlexDenisov, 2014-04-17
Hi Joshua, thank you for this post. I think you should take a look at `xcrun`. This tool may help you to get rid of magic strings. So command xcrun -sdk iphonesimulator6.1 -show-sdk-path will print "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk"
anantha rao, 2014-05-26
Hi thnx for this tutorial, However i am trying to run same example in my mac, but i am getting the following error. make window clang clang: error: no input files make: *** [window] Error 1 This is my Makefile content XCODE_BASE=/Applications/Xcode.app/Contents SIMULATOR_BASE=$(XCODE_BASE)/Developer/Platforms/iPhoneSimulator.platform FRAMEWORKS=$(SIMULATOR_BASE)/Developer/SDKs/iPhoneSimulator7.1.sdk/System/Library/Frameworks/ INCLUDES=$(SIMULATOR_BASE)/Developer/SDKs/iPhoneSimulator7.1.sdk/usr/include window: window.m clang -arch i386 \\ -mios-simulator-version-min=6.1 \\ -fobjc-abi-version=2 \\ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk \\ -o window window.m -framework Foundation -framework UIKit Thank you.
Josh, 2014-06-11

Ah, I'm so sorry Anantha, I omitted a backslash from listing 6. It should read:

 window: window.m 
  clang \
  -arch i386 \ 
  -mios-simulator-version-min=6.1 \ 
  -fobjc-abi-version=2 \ 
  -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk \ 
  -o window window.m -framework Foundation -framework UIKit 

In fact, the backslashes in the code listing were there strictly for formatting reasons. So, you can jam all of this onto a single very long line; to simplify (but slightly uglify):

window: window.m
clang -arch i386 -mios-simulator-version-min=6.1 -fobjc-abi-version=2 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk -o window window.m -framework Foundation -framework UIKit

The line break after "window.m" (as well as a hard-tab character) are required by the Make utility.

ardi, 2014-07-04
Very interesting! Do you've any second part of this article, detailing how to create an app bundle, and code-sign it from just the command-line, without creating any xcode project, and without starting Xcode? Or do you know of any web page or document explaining it? I really want to build iOS apps from the command-line, because it's more convenient for me. Please point me in the right direction, because, honestly, your page is perhaps the only webpage in the World explaining iOS development in a language I can understand. ardi
Duckling, 2014-08-29
I tried these steps on IOS 7.1 and looks like there is an error "Download Failed" when Safari tries to download the file to install/run on the device. A bit of googling around revealed some issues with signing the apps is more strictly enforced i iOS7.. Do you have any suggestions for how to sign the app from the command line?
MikeStz, 2014-09-09
Great post! I am trying to build a C library this way. The problem is that the lib contains a lot of implicit type conversions, which is treated as error by clang. Is it possible to disable this behavior of clang? Thanks!
fractalspace, 2014-09-29
Nice, and to the point. I was however looking to build an iPhone app, unsigned, to run on console (yes, its jailbroken).
rustam, 2015-06-05
Thanks a lot for this post. This is exactly I was looking for. I tried to play with clang ast dump feature but it always failed on importing headers from frameworks
Shravya, 2015-07-14
Hey, Nice post. Helped me a lot. The framework and standard include paths are updated I guess so that fails but on including -isysroot and giving the SDK path works :) This post is the only clear and crisp explanation I could find. Great job for that :D Regards, Shravya
Josh, 2015-07-14
Thanks, this has been by far my most popular blog post. I understand that quite a bit has changed since I wrote it for iOS 4 - maybe it's time for an update.
Andrew, 2015-07-29
Fantastic! Can you write more? Will be nice to build real iOS app
Zach, 2016-02-14
Josh,

Great article.

As I'm sure you're aware, some things have changed since 4 - however I'm sure most readers of the page would be able to fill in the holes.

I personally love to build projects this way. However, I can't seem to find a way to easily debug an iOS app (or any other executable) on OS X without an Xcode project.

Would be nice to be able to keep the build external from Xcode, but still leverage Xcode's debugging, and launching to external devices.

Thoughts?

- Zach
Costin, 2016-05-10
With Xcode 7.3 in order to run the application in the simulator from the command line you need to run:
    xcrun simctl spawn booted ./window
cam, 2016-10-03
Hey Josh,

Great blog! Did you know you share the same name as a teen murderer?
Josh, 2016-10-03
Unfortunately, yes, I do know that (unfortunate that it's the case, not that I know it). I can prove pretty conclusively that he and I are very different people, though, if it's ever in doubt.
My Book

I'm the author of the book "Implementing SSL/TLS Using Cryptography and PKI". Like the title says, this is a from-the-ground-up examination of the SSL protocol that provides security, integrity and privacy to most application-level internet protocols, most notably HTTP. I include the source code to a complete working SSL implementation, including the most popular cryptographic algorithms (DES, 3DES, RC4, AES, RSA, DSA, Diffie-Hellman, HMAC, MD5, SHA-1, SHA-256, and ECC), and show how they all fit together to provide transport-layer security.

My Picture

Joshua Davies

Past Posts