Wednesday, June 23, 2010

Chicken Pau Objective-C

Just like local saying: "If you can't buy a Big Mac, you may still settle with Chicken Pau".

Or we might as well call it "Objective C para gente pobre" (Spanish, thanks to Claudio) above title translates to "poor man's Objective-C".

What I mean by poor man's Objective-C is the development using the language which pre-dominantly using the Apple hardware and the latest Apple compiler. That the rich man's version of Objective-C.

But if you are just like me, who is still saving to buy an Apple hardware, may be this approach will be a short term cure for your addiction. We will start with something as simple as an Ubuntu 10.04 Lucid Lynx Linux box, which is far more affordable than Mac boxes. My objective was to be able to have a hands on experience with the Objective-C language, before moving to the platform where you need to invest more bucks. Practically I decided to learn the language using "poor man's Objective-C" (it's "Objective C para gente pobre" -- thanks to Claudio).

Luckily enough, the Objective-C compiler that we use are from the GNU family of compilers (gcc, cc10bj), which by far the most widely used and widely ported compiler family. It's available everywhere for free. Those guys at Apple also uses GNU compilers, which make it easier for us -- that we don't have to install another proprietary tools.

One sad thing though, the GNU compiler set doesn't fully support the Objective-C version 2 which is the case with latest Cocoa platform. But as I said, my objective was to have a hands on experience on the language, you get the feel, familiarize with it, then you can move one when you piggy bank accumulate enough to produce a bang on the fruity stuff.

Let's start. I assume you already have your Ubuntu box installed.

[sourcecode lang="shell"]
$ sudo apt-get install gcc
$ sudo apt-get install gnustep-base-runtime
$ sudo apt-get install gnustep-core-devel
$ sudo apt-get install gobjc
$ sudo apt-get install gnustep-gui-runtime
$ sudo apt-get install libgnustep-gui-dev
[/sourcecode]

Most of the time you just need to answer "Y" for the questions raised.

Let's make it official by writing a hello world application.
[sourcecode lang="c"]
#import <stdio.h>

int main(int argc, char** argv) {
printf("Hello, world!\n\n");
}
[/sourcecode]
Save the file as hello.m

Compile it using our gcc tool:
[sourcecode lang="shell"]
$ gcc hello.m -o test.out
[/sourcecode]

If it compiles successfully, try to run ti.
[sourcecode lang="shell"]
$ ./test.out
Hello, world!

[/sourcecode]

Here we go, a hello world in Objective-C!
Ok, it's a bit cheating, the only difference from the C language in the source code was only the use of import instead of the usual include

[sourcecode lang="c"]
#import <Foundation/Foundation.h>

int main(int argc, char** argv) {
NSLog(@"Hello, world!\n\n");
}
[/sourcecode]

Now, it's a bit hairy to set the compile right, but for now, just follow this:
Compile it using our gcc tool:
[sourcecode lang="shell"]
$ gcc hello.m -I /usr/include/GNUstep -fconstant-string-class=NSConstantString -lobjc -lgnustep-base -o test.out
[/sourcecode]

If it compiles successfully, try to run it.
[sourcecode lang="shell"]
$ ./test.out
2010-06-23 18:25:06.816 test.out[4981] Hello, world!
[/sourcecode]

Hooray, now we have the development environment that runs the Cocoa Foundation framework!

No comments:

Post a Comment