2

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.

Community
  • 1
  • 1
compound eye
  • 1,898
  • 18
  • 23

3 Answers3

1

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).

Aleksandr Dubinsky
  • 22,436
  • 15
  • 82
  • 99
-1

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    He means _a_ preprocessor, not the c preprocessor. – Aleksandr Dubinsky Oct 12 '12 at 07:13
  • @AleksandrDubinsky Clarified answer – Some programmer dude Oct 12 '12 at 07:22
  • Your last paragraph is the most pertinent. Even today (and actually _increasingly_), the ObjC compiler does a lot of "rewriting" of language features into C functions. The fundamental piece of ObjC syntax, the message send, is the prime example -- `[obj message:arg]` becomes `objc_msgSend(obj, message, arg)` -- but newer bits like `@synchronized`, `@autoreleasepool`, and the [new literal object syntax](http://stackoverflow.com/q/9693647/) are all just translated into lower-level forms. – jscs Oct 12 '12 at 08:15
  • that's quite an interesting answer josh, and I would like to know more detail. – compound eye Oct 12 '12 at 11:55
-1

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.

Mr Bonjour
  • 3,330
  • 2
  • 23
  • 46
  • Author knows OO is possible in C, and is asking for an aid. (A note on English grammar: if the sentence starts with "It is" rather than "Is it" and has no question mark, it is not a question.) GCC does not convert ObjC to C. – Aleksandr Dubinsky Oct 12 '12 at 08:44