Learning Objective C without a Mac - objective-c

I do not have a Mac, or an iPhone. However, the concept of taking C and making it more dynamic towards the idea of smalltalk, python or ruby is really attractive to me. I'd love to start on objective C.
Is objective C just a syntax superset of C or is it really like C. As in, can it be compiled with gcc etc..?
I do most of my programming in Ruby. Objective C seems so much more forgiving than C. You can write native extensions for Ruby in C. Can you write native extensions for Ruby in Objective C?
How can I get started with Objective C outside of owning an Mac/iPhone and having XCode?

Wikipedia says
Today it is used primarily on Apple's
Mac OS X and iPhone OS, two
environments based on, although not
compliant with, the OpenStep standard,
and it is the primary language used
for Apple's Cocoa API though it was
originally used as the main language
on NeXT's NeXTSTEP OS. Generic
Objective-C programs which do not make
use of these libraries can also be
compiled for any system supported by
gcc, which includes an Objective-C
compiler.
Objective-C today is often used in
tandem with a fixed library of
standard objects (often known as a
"kit" or "framework"), such as Cocoa
or GNUstep.
Maybe you should get a Linux vm if you are mainly running Windows and get GNUStep running.

apple contributed the objective c changes back to gcc (not that they had much choice, being GPL). So wherever you can set up gcc, you can set up an objective-c compiler.
Beyond that, the Gnustep environment can give you a bit of the flavor of the original OpenStep/NextStep environment.

Yes, you can write, compile and run objective c programs outside a MAC/IPhone
Please check the link below for details
http://www.otierney.net/objective-c.html#settingup
http://www.faqs.org/faqs/computer-lang/Objective-C/faq/

There's a difference between Objective-C the language, and the frameworks that Apple has built on top of it. Those frameworks consist of a bunch of objects which you can use in your code and are especially helpful if you're targeting the Mac or the iPhone. There are some similar objects for parts of it in GNUStep, but it's not a complete replacement for the Cocoa/etc library and especially doesn't cover anything iPhone related.
GNUStep and the Objective-C language (via gcc) are available for many platforms, and you can write your own programs that use Objective-C objects and interact with any C api from Objective-C.
Objective-C is a superset of C and so is not really more forgiving than C, but it is arguably more forgiving than C++ (an almost-superset of C). You can also mix Objective-C and C++ with GCC but that gets a little complicated, especially regarding exception handling.
For getting started and playing around with the language though, GNUStep and GCC would be a good way to go.

I don't see any compelling reason why you cannot use Objective C for native ruby extensions, except that you won't benefit much. You will still have to use the lower level C Api calls, since as far as i know, there aren't any ruby to objective-c bindings. Additionally, Objective C comes with a fairly substantial library of utility classes that, while helpful, won't easily justify their size and weight in the context of a ruby extension.

You can you this page for simple experiments with ObjC: http://www.codesign.cz/LearnObjC/

Related

Can Objective-C compile to C?

Can I compile an Objective-C program to C?
I'm interested if this can be done so that it can be compiled with other C compilers.
I am aware that GCC can compile Objective-C.
Sure. For example, POC compiles Objective C to plain C code, and the recommended way to use it is to then compile that C code with gcc.
Of course the generated C is an unreadable mess, consisting mostly of calls (through generated extern declarations) to objc_runtime calls, so you'll need to link that runtime to your compiled code.
There's also an LLVM backend that generates C code from any frontend. So, you can plug in the clang Objective C frontend and the C backend and build your own ObjC-to-C compiler. The result will probably be even more unreadable than what POC generates.
Of course most people don't want to compile Objective C*, they want to compile "Objective C 4.0".** POC won't do that; only clang and llvm-gcc will (and in the future, most likely only clang). And they don't just want an Objective C runtime, they want the OS X or the iOS runtime, or at least OpenStep, which again pretty much means clang, llvm-gcc, or gcc.
The big question is, why do you want to do this? You will need to have an Objective C runtime to link to, and your C code will have to be compiled in a way that's ABI-compatible with the way that runtime was compiled, so… I'm not sure what you're going to gain over just compiling from ObjC to native code with gcc or clang in the first place.
* As in the language defined in Brad Cox's 1986 book and/or in his 1991 followup.
** Note that Apple explicitly decided not to call their latest revisions to the language "Objective-C 4.0"… or anything else. The last version with a name, or a real specification, was "The Objective-C 2.0 Programming Language". But Apple has continued to add features, and the "unnamed feature set that came in with the LLVM 4.0 frontend for Objective-C 2.0" is a bit of a mouthful.
Technically, yes, but it'll be really ugly. LLVM had a C backend — i.e. it could compile to C — up to version 3.1. There have been efforts to revive it but I can't speak as to their progress. There's also a C++ backend that is better maintained but doesn't quite do the same thing.
However you'll have the obvious problem that none of the Foundation libraries will be available on your target platform. No NSString, no NSArray, etc. Once you've found a working implementation of those you'll also have found a working Objective-C compiler.

Do any tools exist that allow Objective-C syntax to be processed to object oriented pure C?

It is possible to do OO programming in pure C.
Some strategies use pre-processor macros to make it easier and less error prone. Some strategies involve adding new syntax which is expanded to pure c by a pre-processor, along with a base object class and some methods for memory management.
It seems that Objective-C began as a project much like this
Do any tools exist that allow objective-c syntax to be processed to pure C?
Without having explored it, it seems do-able.
Just to clarify, I am not asking about compiling iOS code to other platforms, or asking about ports of the cocoa library to other platforms, I am wondering about ways of using oo techniques in pure-C, using Objective-C syntax and a preprocessor or precompilation step.
Portable Object Compiler. It's not capable of compiling modern Objective-C, but it sounds like it is perfect for what you're asking. Look here at a discussion of POC's shortcomings
The situation for C++ is more interesting. Cfront was the original C++ compiler that produced C code, but besides being long outdated it was commercial and cannot be (easily?) downloaded today. Fortunately, there is Comeau C/C++ which is supposedly very modern and standards compliant. It costs $50.
However, I wouldn't expect to get very readable C code from either of them (especially the full-featured Comeau).
It is possible to do oo programming in pure c?
Yes, as oo is a matter of philosophy. Look at glib and how you can do c style object: http://developer.gnome.org/glib/
Apple did it with Core Fundation: https://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFDesignConcepts/CFDesignConcepts.html#//apple_ref/doc/uid/10000122i
By the way: Do any tools exist that allow objective-c syntax to be processed to object oriented pure c?
Yeah: gcc (GCC 4.6 according to Wiki, never actually checked ;)) and clang, tools that you are usin everyday.
This is the Objective-C Runtime who make the obj-c obj-cAble, so you need libobjc.A.dylib library too. You can write obj-c in pure c code, since all message '[]', '#' directive and other obj-c stuff are converted in c after compilation.
No it's not possible, as all special (non-identifier) symbols of Objective-C can not be used as preprocessor macros. At least not with the standard C preprocessor.
Other preprocessors may be able to define macros with non-identifier names, although I don't know of any.
When talking about preprocessors in the early days of Objective-C (and also about C++) it's probably (and in the case of C++, definitively) a custom made parser that instead of outputting assembler or objective code outputted C code.

objective c on windows and gcc

Is the gnu objective c compiler for windows the same as the compiler for Mac OS X (and Ipad)?
I want to write some code on windows and run it on the mac and ipad, objective C seems to be the best way to go for the Mac/Ipad but is the gnu compiler the same? I know the libraries cocoa etc are not available but this is not an issue we'll be writing different interface code for each platform (please no comments about this is not the right way etc).
tia,
Dave
The compiler is more or less the same, but the runtime systems are different. If you don’t use the new features from Objective-C 2.0 your code should work fine with both runtime systems. If you want to use the new features you have to use a different runtime. You can find more information here on the GNUstepWiki.
But you still need a framework that provides basic objects like strings, arrays, dictionaries and so on. You could use this part from The Cocotron, a framework that tries to make all of Cocoa available on Windows, Linux and other platforms.

Is Objective-C used without Cocoa?

It seems that Cocoa seems to be the main platform for Objective-C. GCC (which Xcode uses) supports Objective-C so it must be available on a wide range of platforms.
Are there any notable cross-platform projects that use Objective-C but not Cocoa (or its open source cousin GNUStep)? Is it really used outside the Apple ecosystem?
Objective-C has also been popular in the scientific and financial services communities. There are still many Objective-C based applications deployed in banking, mostly on the trading analysis front. A friend works on a nearly million line of code Objective-C based analysis and trading engine for which they have written their own class hierarchy from scratch.
At one point, one of the more popular Linux window managers was written in Objective-C. That was a few years ago and may no longer be the case.
The LLVM compiler also compiles Objective-C, including having full support for Blocks. It is quite portable, too.
There is also David Stes's Portable Object Compiler. It compiles Objective-C to C and uses a class hierarchy that is based quite directly upon the original ICPak class hierarchy from StepStone.
It seems what you're asking is if Objective-C is ever used without Cocoa, or GNUstep, or Cocotron, or any API like them.
The answer is basically no. Without an API like Cocoa (or GNUstep, etc.), Objective-C isn't very useful. For example, without NSObject, retain and release wouldn't even exist. A ton of very important features are built into these APIs, so it's somewhat pointless to use Obj-C without one of them.
Yep, there sure are. The one I can think of from the top of my head is Cocotron. An effort to port AppKit to Windows.
http://www.cocotron.org/
Aside from that, Objective-C can be used on any platform that gcc will run on. You won't have the wealth of frameworks that are available in Cocoa on OS X or the iPhone, although as you mentioned, GNUStep does a pretty good effort.

Game programming on Objective-C and linux

I'd like to give Objective-C a try in game programming. The problem is I'd need some tools and libraries for this.
First thing that comes into mind I need is the GUI and graphics library.
What choices do I have?
There are Four non Mac implementations of OpenStep/Cocoa type Objective C platforms:
OpenStep/NextStep : Openstep/NeXT support machines only - Discontinued.
p..... can remember name or find it on web - it's out there somewhere ! - Linux
Cocotron - Windows only
GNUStep Linux/Windows
GNUSTep is the way to go for Linux... I mention there others because 2 and 3 are available in source and they can be used to patch holes in code the GNUStep doesn't have and they might.
When it comes to Graphics library - you may have problems because Apple moved away from the old OpenStep APIs and has kept evolving their APIs far faster than the GNUStep people can play catch up. There is no support for the latest graphics libraries such as Core Animation.
As Objective C is C and C++ friendly you can use any of these libraries as is, or by making the Objective C friendly by making a wrapper.
You may or may not want to be Mac compatible and do versions for that so it may not be important.
If you do you might want to do a Linux Wrapper for something like OpenGL if it is missing, which I think it is but have a check yourself....
There are Linux development tools including a project IDE and GUI builder for GNUStep on Linux.
Tony
I believe you're asking for GNUStep.
SDL is a C library which is popular for game programming.
If you're looking for Foundation/AppKit stuff you can also look at Cocotron. I've never tried it though.
Supporting SDL there are Objective-C bindings here. However since Objective-C is a thin OO layer on top of C you can use most of the existing C libs for game development, SDL, Clanlib, etc...