Monday, June 8, 2009

Jumpstart on iPhone with Objective-C in Windows

Eager to learn about building an iPhone app but not quite ready to set aside the money for a Mac? That's okay, you can still get started using the Objective-C programming language right away. Objective-C is the language used to write iPhone, iPod Touch, and Mac applications. Luckily it is supported by gcc (GNU Compiler Collection), which is available on many platforms.

If you have a Windows box handy, then you just need gcc with Objective-C support. The easiest way to do that in Windows is with MinGW, Minimalist GNU for Windows. The download page should have an Automated MinGW Installer for Windows.

Once you've installed MinGW, you can add the binaries to your path or use a build file of sorts; there are lots of options depending on your taste.

Let's see a basic Hello World app; we'll call it Main.m (m is the preferred file extension for Objective-C programs):
// From http://codewandering.blogspot.com/
// Main.m
int main(void)
{
printf("Hello, world!\n");
}
Look familiar? Objective-C is a proper superset of the C language, so valid C code is valid Objective-C code. Of course, to get the full power of Objective-C you'll want to use the object-oriented programming aspects of Objective-C as well.

A simple batch file like this will compile the program:
@rem From http://codewandering.blogspot.com/
setlocal
set path=%path%;c:\MinGW\bin\
gcc.exe -x objective-c Main.m -lobjc
endlocal
You may need to modify the set path command if you specified a different home folder for MinGW.

Now just run the batch file from a command line, which produces a.exe, your executable program. If you run that, you should see:
Hello, world!
Ready for more? Check out these great resources:

4 comments:

Anonymous said...

this tutorial sucks. doesn't even mention how to use the foundation framework.

Will said...

Feedback is welcome, but please keep it constructive.

I might agree with the analysis by Anonymous if this were a tutorial or purported to be one, or if the information was wrong when it was published. It is not, it does not, and it was not. It is merely a brief blog post. And 8 months old. Back when I wrote it, I was trying to do a little Objective-C on Windows. I researched on the web about how to set that up, but it was difficult to locate good information. I decided once I got everything figured out (which compiler was best, how to pass it the right arguments to build Objective-C, test that it actually worked...), in the spirit of cooperation, I might as well share it with others attempting the same. I also included links to further Objective-C and iPhone information, to help the reader once they got their compiler up and running.

8 months is an eternity in iPhone development. There may very well exist actual Windows Objective-C tutorials now in 2010. If anyone has a good one, please feel free to include a link here. Then we can all benefit.

Peter said...

If you just want to experiment, there's an Objective-C compiler for .NET (Windows) here: http://www.qckapp.com/index.html?p=ObjC

Anonymous said...

Mysteric , when i compile this, i was first force to write

#import


int main( int argc, const char *argv[] )
{
printf("Hello, world!\n");
return 0;
}

then it works, it produce an a.exe file.
When i try to execute, it complain about missing file:

libgcc_s_dw2-1.dll

what ??

It doesn't work.
I have install the latest gcc-objective-c what could possible be missing ?

Copyright 2011 by William Cain