Integrating Emacs and XCode

I tend to use Emacs for all my editing, although when I am writing Mac OS/X GUIs, I couple it with XCode for managing the project build process. The main things I’ve needed to customize in my “.emacs” file is to get Emacs to automatically recognise Objective-C files when they are opened (including Objective-C “.h” files), and have the “compile” command in Emacs call xcodebuild to actually compile the program.

To have Emacs recognise Objective-C files, first add the following to your “.emacs” so that “.m” and “.mm” files get handled automatically:

(setq auto-mode-alist
      (cons '("\\.m$" . objc-mode) auto-mode-alist))
(setq auto-mode-alist
      (cons '("\\.mm$" . objc-mode) auto-mode-alist))

Next add this code so that header files get automatically handled.

Finally you can compile using xcodebuild with the following code:

(defun bh-compile ()
  (let ((df (directory-files "."))
        (has-proj-file nil)
    (while (and df (not has-proj-file))
      (let ((fn (car df)))
        (if (> (length fn) 10)
            (if (string-equal (substring fn -10) ".xcodeproj")
                (setq has-proj-file t)
      (setq df (cdr df))
    (if has-proj-file
        (compile "xcodebuild -configuration Debug")
      (compile "make")

4 Replies to “Integrating Emacs and XCode”

  1. This comment is about your site (or at least the WordPress layout), not this post; I couldn’t find any other way to contact you.

    What I wanted to let you know is that when you click the menus along the top, the tabs (e.g. All/Uncategorized/Fun/etc.) overlay it, making it impossible to see or click on everything in the menu.

  2. Great. I changed my alists a long time ago, though I still have to thump the mode for .h files in mixed C++/objc-proj manually. However I just discovered xcodebuild and your elisp gives me some good ideas. Thanks for this.

Leave a Reply