Playing with Objective C on Debian

By uliwitness

[Debian showing Objective C source code in gedit]

I felt like playing with Linux a bit, so I went and installed Debian on a partition. Apart from a few failed attempts to install it on an external drive (something which works fine with MacOS X, so I was spoiled) and a bit of confusion when it asked me for my drive name (how would I know a cryptic character combination like hdb4? And I selected that drive in your UI before, can’t you let me use that same selector again?), it went pretty smooth.

Once having installed Debian, I wanted to play a little with GCC on there. However, by default, like a nice desktop OS, the developer tools weren’t installed, so I opened a root Terminal window and typed in

apt-get install gcc cpp binutils libc6-dev make gobjc

Most of the items after install are what any web site on Debian will tell you is needed to use GCC: GCC itself, the C preprocessor, binutils (mainly for the ld linker and the as assembler), the C standard library and the make command line tool for making it easier to build complex build commands (think a command line version of Xcode project files).

But the last one, gobjc, installs the GNU Objective-C runtime and compiler. This is only the runtime with the old Object base class, i.e. without -retain and -release. You get Object by including objc/Object.h. You’ll also want to add a -lobjc option to your GCC command line, or you’ll get lots of error message about missing objc_msgSend() etc.

These headers get installed in /usr/lib/gxx/x86_64-linux-gnu/4.3/include (or whatever GCC version you installed, and whatever architecture your Mac has), in case you want to find out how Object looks..

To get a more familiar Foundation “framework”, I built and installed libNuFound, a Foundation clone that works alongside the GNU runtime, written for use with the Nu programming language. The basic installation is detailed on their github page, essentially the traditional

make install

dance, except that you need to copy the headers yourself:

cp -r Foundation /usr/local/include

Then, if you wanted to build a Foundation tool whose source code is in a file main.m, what you need to do is:

export LD_RUN_PATH="$LD_RUN_PATH:/usr/local/lib"gcc main.m -lobjc -lNuFound -lm -ldl -fconstant-strings-class=NSConstantString

The thing with LD_RUN_PATH is needed so the linker can write the full path of the library into the executable. Otherwise you get an error like

error while loading shared libraries: cannot open shared object file:No such file or directory

There are other options to solve this problem that make will tell you about when you build/install libNuFound. The -lobjc option pulls in the GNU ObjC library, -lNuFound grabs the Foundation library we just built, and -lm and -ldl grabs the standard C math library and a code-loading library needed by libNuFound. The last parameter tells the ObjC compiler that Objective C string constants like @”Cool” should generate NSConstantString objects, not the old String flavor.

But hey, now I have a Debian installation that runs Objective-C code. Neat :-) No UI libraries, though.

Update: Note that you will probably want to install this on a 32-bit CPU with a 32-bit Debian. With some investigation, Mr. Mo and me found that libNuFound seems to have a few bugs on 64-bit CPUs at the moment.


2 Comments Leave a comment

  1. Have you made a conscious decision against GNUstep? Despite their name, they’re pretty OS-X-compatible nowadays and they *do* have GUI libraries.

    No UIKit yet, though.

    • In short: Yes. I was looking for a lightweight library. The GNUstep guys still can’t decide whether they want to be an OS distribution or a library. They behave like an OS (lots of daemons, non-OS-native folder structures etc.) but insist they’re a library.

Share your thoughts