Sunday, November 14, 2010

Getting Started: Programming Objective C on Windows

Why Learn Objective C?

Objective C has made C programming cool again and it's making a comeback. I've been interested in learning Objective C for many reasons: iPhone Development, statically compiled language with dynamic features and to brush up on my C programming.

It's been many years since I've done any C or C++ programming. If the C language gave birth to C++ then Objective C would be its sibling. Current versions of Objective C is basically the C language sandwiched with Smalltalk sprinkled with the goodness of dynamic programming and syntactic sugar.

Many people struggle writting iPhone applications in my opinion because they neglect the fact that Objective C is a first class language and like C# (.Net) or Java require time and patience to learn it first before learning the frameworks and paradigms. This would be akin to trying to learn C#, HTML & JQuery for the first time on an ASP.Net MVC project.

I will be treating Objective C like a first class language and I will be learning it on Windows first because I want to focus on the language first and not the frameworks (such as Cocoa). My first goal is to get a running development environment on Windows for Objective C programming, write a simple "Hello World" app and compile it using both gcc and a make file.

Setting up Development Environment on Windows from scratch

GNUstep is the cross platform, free & open source version of Objective C. First download and install GNUstep Core, GNUstep System, GNUstep Development Environment and your favorite text editor such as Notepad++. After installing this a group will be added to your Start menu in Windows named GNUstep, beneath it is a shortcut to a "Shell". Once open, you will be in a full command shell (BASH) you will be able to compile Objective C code files using make or gcc but we will first have to create a class file

The gcc that comes with GNUstep can compile C or Objective C apps. We will first start with a Hello World app that uses C header files and then tweak it to use Objective C header files to make sure everything is lined up and configured correctly in your development environment. Create a file called main.m and paste the following into it.

Within the GNUstep Shell navigate to the same folder and compile it (within GNUstep shell C:\YourProject would be /c/YourProject) by typing the following:

Then test the output in shell by running the app and make sure the output is correct before continuing.

Next we will modify the helloworld source to use Objective C library and make a build file for it so that it will compile. First, update main.m to below:

GNUstep provides makefiles which include the references to the libraries and folders that you will need. Create a file named GNUmakefile in our project directory and put the following into it.

Now to build it is simple, just type the following into our shell.

Some warnings should appear from the compiler but it should compile. Run the output which should be in a folder named obj and verify the output. There we go, our first Objective C app. For more info on make files within GNUstep see the reference manual.

6 comments:

Thanks a lot for the manual, it is really helpful. The truth is that everything works as it is written until the time that you execute make on the shell. I receive the following error"make: no targets specified and no makefile found. stop"Any idea, why is this happening?