« NuTitleCaseIndiana Jones and the End of a Franchise »

Mixing Nu and Objective-C

It’s no secret that I’m a big fan of Nu, the new programming language based on Lisp and built on top of Cocoa. I’ve got a reasonable number of small projects and whatnot available that target Nu programmers. I’m also using Nu in Paperclip. I’m also playing around with Nu in other, private applications. I’m most excited about the ability to easily mix Nu and Objective-C in the same project. Today and yesterday, I decided to try to integrate the two in one project from the very beginning. Basically, anything written in Nu is just as available to the run time as anything written in Objective-C. It’s a bit of a chore and requires a bit of setup but it can be done. I’ve provided step-by-step instructions as well as lots and lots of screenshots.

[UPDATE] Following the advice of Drew in the comments, I’ve created a Cocoa-Nu template available for download. It can be found on my github account. A tarball can be download here.

  1. First things first, create a new Cocoa project in Xcode. If you don’t know how to do that, there’s not much point in continuing. This post is targeted towards those with experience programming using Xcode. I’ll point out where things are at, but I won’t provide the hand-holding details. Also, you should be up and running with Nu. Instructions for that are found on the Nu website.

  2. Now, add the Nu framework. Perhaps a bit obvious, but it’s easy to forget about this.

    Add Nu framework

  3. Since I’m planning on distributing this app, let’s go ahead and bundle the framework with our app. We need a new copy files build phase for that. Be sure to set the build phase to copy to the Frameworks subfolder. While I’m at it, I like rename the build phase so that it’s something more meaningful. Don’t forget to copy the Nu.framework into the build phase so that it’s distributed with your application.

    Create a new copy files build phase Change copy location

    Rename the build phase Add Nu framework to the copy build phase

  4. This is an important step. The Nu framework is linked when first built to be only accessible from /Library/Frameworks/. In order to distribute the framework with you application, you need to re-link it. You can do that by adding a new “run script” build phase and using the following shell script:

    install_name_tool -change "Nu.framework/Versions/A/Nu" "@executable_path/../Frameworks/Nu.framework/Versions/A/Nu" $TARGET_BUILD_DIR/$TARGET_NAME.app/Contents/MacOS/$TARGET_NAME
    

    Re-link Nu.framework Shell script

    You may notice that I also rename the build phase as well.

  5. Now that all these stuff is out of the way, we can start integrating Nu and Objective-C. The first thing you’ll want to do is load all of the Nu files. This can be handled easily by making some small changes to main.m. You’ll want to load these files before any of your other files try to reference it. Simply change main.m to the following:

    #import <cocoa /Cocoa.h>
    #import <nu /Nu.h>
    
    
    int main(int argc, char *argv[])
    {
        NSAutoreleasePool *pool = [NSAutoreleasePool new];
        id nu = [Nu parser];
        for (NSString *nuFile in [[NSBundle mainBundle] pathsForResourcesOfType:@"nu" inDirectory:nil])
            [nu eval:[nu parse:[NSString stringWithContentsOfFile:nuFile]]];
        [nu close];
        [pool release];
    
    
    
    return NSApplicationMain(argc,  (const char **) argv);
    
    }

    Update main.m

  6. While we’re at it, we’re probably going to want to generate header files from the Nu files so that we can use them nicely in Objective-C. My nug can do that. Go get it and learn to use it. Once you create the header files, you can import them into Objective-C files and access classes just like you normally would.

    Run nug Integrate with ObjC

  7. Unfortunately, you’re not quite done yet. gcc will whine at you because it doesn’t know where the stuff you’ve defined in Nu classes is. You can tell gcc to stop whining by putting the following into the Other Linker Flags:

    -undefined suppress -force_flat_namespace
    

    Other Linker Flags

That should be it. It’s free coding from here out. You should now be able to write Nu files and add them directly to your project (to be copied into the Resources folder). You can build an entire app by mixing Nu and Objective-C. Remember that you’ll have to rerun nug when you need to update the header files.

3 comments on “Mixing Nu and Objective-C”

[…] Mixing Nu (lisp-ObjC language) and ObjC, I would like to spend some quality time with Nu soon: http://www.fromconcentratesoftware.com/2008/05/24/mixing-nu-and-objective-c/ […]

Peter:

June 16th, 2008 at 12:12 pm

Thanks for writing this up! I’m a new Mac developer but long time Lisp developer and I’ve been wanting try Nu+Object C but needed just this kind of information to get started.

Drew:

June 23rd, 2008 at 3:33 am

I second the thanks for writing this up. I found out if you copy or move the project folder into /Developer/Library/Xcode/Project Templates and rename the folder something like Cocoa-Nu Application it will give you a template for other applications.

Leave a Reply