Ubuntu 12.04 + Objective C + Automatic Reference Counting

Let’s face it, Objective C syntax is kinda wack. But there are some features of objective c that are cool. I like that objc is a super-set C, meaning that you are free to mix C and objc code together. This makes it easy to take advantage of pre-existing libraries written in C and C++. I also like the Foundation library’s reference counting memory management convention. You know – retain/release/autorelease. The simple memory management rules take the confusion out of who owns the object and when it should be freed.

The addition of Automatic Reference Counting (ARC) compiler feature makes it easier to write leak-free code. ARC comes out of the box in XCode 4.5 which is great if you are an iOS/Mac developer. But did you know that ARC is actually implemented in CLang, which is open-source and available on lots of other platforms other than Apple products. This post explains how to get started using objc + ARC on Ubuntu linux.

ARC requires a newer implementation of the objc runtime – specifically ARC requires libobjc2 1.6+. At the time of this writing, the objc runtime that ubuntu 12.04 ships with is the older gcc runtime that does not support ARC. I was forced to compile libobjc2 1.6 myself. I have written a little Makefile that can download, install and build a test app with clang and ARC.

# prereq:
# apt-get install build-essential clang gobjc libicu-dev libxml2-dev libgnutls-dev libssl-dev

.PHONY: clean

BUILDDIR := $(shell pwd)/build
MAKE := gnustep-make-2.6.2.tar.gz
GNUSTEPBASE := gnustep-base-1.24.0.tar.gz
TAR := libobjc2-1.6.1.tgz
GNUSTEPCONFIG := build/bin/gnustep-config
BASELIB := build/lib/libgnustep-base.so
LIB := build/lib/libobjc.a

-rm -rf $(BUILD_DIR)
-rm *.d
-rm testnoarc testarc

wget ftp://ftp.gnustep.org/pub/gnustep/core/gnustep-make-2.6.2.tar.gz

wget ftp://ftp.gnustep.org/pub/gnustep/core/gnustep-base-1.24.0.tar.gz

wget http://download.gna.org/gnustep/libobjc2-1.6.1.tgz

cd gnustep-make-2.6.2 && \
export CC="clang" && \
export CXX="clang++" && \
./configure –prefix="$(BUILD_DIR)" && \
make install

cd libobjc2-1.6.1 && \
DIR)/share/GNUstep/Makefiles/GNUstep.sh && \
CC="clang" CXX="clang++" PREFIX="$(BUILD_DIR)" make -f Makefile install

cd gnustep-base-1.24.0 && \
. $(BUILDDIR)/share/GNUstep/Makefiles/GNUstep.sh && \
CC="clang" CXX="clang++" ./configure –prefix="$(BUILD
DIR)" && \
make && make install

gcc -I/usr/include/GNUstep main.m Fraction.m -o testnoarc -lgnustep-base -lobjc

. $(BUILD_DIR)/share/GNUstep/Makefiles/GNUstep.sh && \
clang gnustep-config –debug-flags -fobjc-arc -fobjc-nonfragile-abi mainarc.m Fraction.m \
-o testarc gnustep-config –base-libs


Special thanks:

To me, I consider garbage collection a near-perfect memory management system. (I know, I know, you are gonna say, performance is an issue with garbage collection right? But c’on modern garbage collecter implementations are pretty badass) Anyways, garbage collection is still pretty bulky and often times requires hardware-specific features to work well. It might mean that an app written using a garbage collection system is not as portable as you might have hoped it to be. So reference counting seems like a nice compromise if garbage collection is not available. Of course, with reference counting you still need to deal with the circular pointer problem.

Paul Soucy

Read more posts by this author.