A poster on the nxtasy.org forum asked about using NXC in Xcode. I wrote this tutorial about how to get NXC to work in Xcode 3. There is a similar tutorial available for NQC in Xcode, which is the inspiration for this tutorial. NXC is simply C code, so Xcode provides a syntax-aware editor that allows you to write NXC code and then compile and download the program to the NXT from within Xcode.
Before you begin you will need a copy of Xcode, Apple’s development IDE for Mac OS. A copy of Xcode is located on your Mac OS 10.5 install DVD. Insert the DVD and navigate to the XcodeTools package, double-click on it to launch the installer. Full details of how to install Xcode on Mac OS 10.5 are here.
The NXC compiler for Mac OS is available at http://bricxcc.sourceforge.net/nbc/. Download the Mac OS X installer image, which is currently named nbc-1.0.1.b35.osx.tgz and save it into a folder named NXC. Double-click on the archive file to extract the contents. By default the compiler files are extracted to a folder named ‘nxt’, the contents of the nxt folder are shown below (I saved the files to a folder on my Desktop):
The NXC compiler executable is named nbc, and the program to download the compiled file to the NXT is named nxtcom. These two programs are UNIX executables; you would normallyrun them from the Terminal. In the next step you will configure Xcode to call these programs to compile your NXC source.
How does it work?
The Xcode window shown below shows the Makefile for a sample Helloworld NXC project. You can add source files into the source folder in the left-hand Groups & Files pane by right-clicking and selecting New File…
A Makefile specifies a target for the build, and a set of dependencies. The Makefile for the Helloworld program looks as follows:
# Path to the NXC compiler relative to the Makefile
# Options to pass to the compiler
# Change the name of the program helloworld.rxe to be whatever you want
# to name the final executable
all: $(PROGRAM).rxe download
$(PROGRAM).rxe: source/$(PROGRAM).nxc Makefile
cd source; \
../$(NXC) -O=../build/$(PROGRAM).rxe \
cd build; \
../$(NXTCOM) $(USB) $(PROGRAM).rxe
/bin/rm -vf build/$(PROGRAM).rxe
When you use this Makefile change the variable PROGRAM to be the name of your executable. For example, if you had created a program named Turtle, you would change the line to read:
and the Makefile will compile turtle.nxc to turtle.rxe.
In Xcode click on the Build icon (the hammer) to launch the build process. If all goes well then you will see the text “Succeeded” in the bottom right corner of the Xcode window. If you have errors (e.g. typos in your NXC file or the NXT is not connected) then the text “Failed” will be displayed in the bottom right hand corner. Click on the text “Failed” and he Build Results window appears to show you the output from the NXC compiler.
In the Build Results window shown below I did not have my NXT turned on, so the compile succeeded but the download step failed:
I’ve created a simple Xcode project named Helloworld Xcode Project.zip that contains a single source file (Helloworld.nxc) and compiles and downloads it to the NXT via the USB cable. The nxc source files are placed in the source directory, and the executable rxe file gets compiled into the build directory. The Makefile controls how the build process works.
Save the zip file into a folder on your desktop, double-click on it to extract the files and double-click Helloworld.xcodeproj to launch Xcode.