Last Updated: February 25, 2016
· mxcl

Scripting Objective-C code with Clang

Make the script executable. Leave off the extension.

It works by using AWK to get the objc portion and then creating a new tmp file that is compiled and executed. When the program is executed we immediately delete the two temporary files.

For me this works faster than Xcode or a Makefile. Clang is that fast.

  • Cons: compile error line numbers are off by the SHELL portion.
  • Pros: it's cool.

OBJC=`awk '/^__OBJC__/ {print NR + 1; exit 0; }' $0`
tail -n+$OBJC $0 > /tmp/YOLOtests.m

clang -ObjC -F$D -I. -fmodules -fobjc-arc \
    -framework SenTestingKit \
    /tmp/YOLOtests.m *.m -o /tmp/YOLOtests

install_name_tool -change \
    @rpath/SenTestingKit.framework/Versions/A/SenTestingKit \
    $D/SenTestingKit.framework/SenTestingKit \

exec /tmp/YOLOtests


#import <SenTestingKit/SenTestingKit.h>
#import "YOLO.h"

int main() {
    unlink("/tmp/YOLOtests.m");  //before or… doesn't happen! FFS

    @autoreleasepool {

Real project example: