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.

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

.PHONY: clean

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

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

$(GNUSTEP_MAKE):
wget ftp://ftp.gnustep.org/pub/gnustep/core/gnustep-make-2.6.2.tar.gz

$(GNUSTEP_BASE):
wget ftp://ftp.gnustep.org/pub/gnustep/core/gnustep-base-1.24.0.tar.gz

$(LIBOBJC2_TAR):
wget http://download.gna.org/gnustep/libobjc2-1.6.1.tgz

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

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

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

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

testarc: $(GNUSTEPBASELIB) $(GNUSTEPCONFIG) $(OBJC2LIB)
. $(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

[/code]

Special thanks:
http://wiki.gnustep.org/index.php/GNUstepunderUbuntuLinux
http://wiki.gnustep.org/index.php/ObjC2
FAQ

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.