Objective-C without Cocoa

There are people who think that writing codes without Cocoa is impossible, or at least it is meaningless. Although many articles on Objective-C mentions that it is a superset of the C, many people really don’t understand it.

Some even ask, “Is it possible to use functions from libraries written in C with Objective-C code?”.

Not so few people are curious about why Objective-C didn’t become popular, and some answer that it is because of NeXT’s falling down. Their reasoning is based on the NeXT’s failure in making their system popular. Is it really so?

I don’t think, so. Just like standard C libary or C++ library, anyone who are interested in making Objective-C standard library could build their own.

However, it didn’t take place. You can use standard C library in Objective-C code. So, probably the original writers of the Objective-C thought it would be OK not to have “standard” library for the Objective-C.

Anyway, truth is that you can write codes with the Objective-C and standard C library. You can write your own Objective-C class which is analogous to the NSObject. Or you can inherit from the Object. (Actually, the Object was the root class before when NSObject became so. )

The gcc provides some header files to help coding in Objective-C :

  • encoding.h
  • hash.h
  • NXConstStr.h
  • objc.h
  • objc-api.h
  • objc-list.h
  • Object.h
  • Protocol.h
  • sarray.h
  • thr.h
  • typedstream.h

Among them, application programmers will be interested in Object.h, Protocol.h, thr.h, hash.h, and NXConstStr.h. The Object.h contains a class, Object, like the NSObject. Thus, it has messages like, init, initialize, free, alloc, and so on. thr.h is for threading.

So, with those header files, you can write codes in Objective-C.

The Objective-C is really a little addition to the C, while it has the genuine power of the Objected Oriented Programming.

Although it doesn’t support operator overloading, meta programming, it supports dynamism, message forwarding, remote messaging, flexible expansion of class.

However, as for reusability, I personally think that Objective-C is better than the C++.

Now, here is a simple Objective-C code without Cocoa or Foundation.

To compile it, issue :

    gcc -g -O -c MyClass.m main.m

    gcc -fgnu-runtime -fobjc-exceptions MyClass.o main.o -lobjc

File : MyClass.h

#import <objc/Object.h>
#import <objc/objc-api.h>
#import <objc/NXConstStr.h>
#import <objc/thr.h>


int gWait;

@interface MyClass : Object
{
    objc_mutex_t pMutex;

    NXConstantString *description;
    int value;
}

- (void) setDescription:(NXConstantString *)theText;
- (void) description;
- (void) doSomethingAtomically;
- (void) doSomethingAtomically1;
- free;
@end

File : MyClass.m

#import “MyClass.h”
#import
#include

extern int gWait;

@implementation MyClass

– (void)setDescription:(NXConstantString *)theText
{
description = [theText copy];
value = 0;
}

– (void)description
{
printf(“%s”, [description cString]);
}

– (void) doSomethingAtomically
{
int i;

printf(“in Thread function\n” );

objc_mutex_lock( pMutex );

for( i = 0; i < 5; i++ ) { value++; printf("1st : Thread %d's value = %d\n", objc_thread_id(), value ); } printf("\n"); objc_mutex_unlock( pMutex ); Sleep( 3 ); objc_mutex_lock( pMutex ); for( i = 0; i < 5; i++ ) { value++; printf("2nd : Thread %d's value = %d\n", objc_thread_id(), value ); } printf("\n"); objc_mutex_unlock( pMutex ); Sleep( 3 ); objc_mutex_lock( pMutex ); for( i = 0; i < 5; i++ ) { value++; printf("3rd : Thread %d's value = %d\n", objc_thread_id(), value ); } printf("\n"); objc_mutex_unlock( pMutex ); gWait++; } - (void) doSomethingAtomically1 { int i; printf("\t in Thread function 2\n" ); objc_mutex_lock( pMutex ); for( i = 0; i < 5; i++ ) { value++; printf("\t 2-1st : Thread %d's value = %d\n", objc_thread_id(), value ); } printf("\n"); objc_mutex_unlock( pMutex ); Sleep( 3 ); objc_mutex_lock( pMutex ); for( i = 0; i < 5; i++ ) { value++; printf("\t 2-2nd : Thread %d's value = %d\n", objc_thread_id(), value ); } printf("\n"); objc_mutex_unlock( pMutex ); Sleep( 3 ); objc_mutex_lock( pMutex ); for( i = 0; i < 5; i++ ) { value++; printf("\t 2-3rd : Thread %d's value = %d\n", objc_thread_id(), value ); } printf("\n"); objc_mutex_unlock( pMutex ); gWait++; } - (id)init { if( [super init] != nil ) { pMutex = objc_mutex_allocate(); description = nil; } gWait = 0; return self; } - free { printf("being deallocated..\n"); objc_mutex_deallocate( pMutex ); if( description ) { [description free]; } } @end [/sourcecode] File : main.m

#import
#import “MyClass.h”
#include

void isMultithreaded(void)
{
printf(“The program is multithreaded\n”);
}

int main( void )
{
MyClass *myObject = [[MyClass alloc] init];

[myObject setDescription:@”Hello, World\n”];
[myObject description];

objc_set_thread_callback( isMultithreaded);

objc_thread_detach( @selector(doSomethingAtomically), myObject, nil);
objc_thread_detach( @selector(doSomethingAtomically1), myObject, nil);
objc_thread_detach( @selector(doSomethingAtomically), myObject, nil);

objc_thread_yield();

while( gWait < 2) { //printf("main : Shall I sleep?\n"); Sleep(5); } [myObject free]; return 0; } [/sourcecode] NOTE : Above codes were written in MingW environment.

6 responses to this post.

  1. Posted by Darcy on April 4, 2009 at 9:06 AM

    Thanks very much for
    gcc -fgnu-runtime -fobjc-exceptions MyClass.o main.o -lobjc

    Reply

  2. Posted by cap on June 30, 2009 at 7:54 AM

    Hi,

    Using default gcc (i686-apple-darwin9-gcc-4.0.1) that comes with Leopard,
    there is no thr.h in /usr/include/objc
    while there is /usr/include/objc/objc.h

    Do you know why ?

    Thx

    Reply

    • Posted by jongampark on June 30, 2009 at 8:34 AM

      Hi, cap.

      I don’t know why Apple removed some of the header files. Did you check the source codes from the GCC?
      Maybe there was some changes for the Objective-C 2.0 or Apple Inc didn’t want to people to use the header files…
      I don’t know. You can ask to Apple’s Obj-C mailing list and post any answers here or can check the GCC site.

      Reply

    • Posted by jongampark on June 30, 2009 at 8:46 AM

      I just checked GCC’s trunk located at http://gcc.gnu.org/viewcvs/trunk/libobjc
      There are still thr*.c files and they are just 2 months old.
      Also, in http:///gcc.gnu.org/viewcvs/trunk/gcc/objc, I don’t see any thr*.h files.
      So, I think there was some changes on GCC side recently.
      You can ask about it to GCC people at the GCC website.

      Please post any answer when you hear from them.

      Thank you.

      Reply

  3. Posted by Cap on June 30, 2009 at 12:03 PM

    “Neither objc_mutex_t nor objc/thr.h exists on Mac OS X.

    There are two Objective-C runtime implementations: the one originally written by NeXT, and the one originally written for GNU. They both implement the same language (more or less), but their internal structure, file formats, and introspection API are incompatible. Mac OS X uses the NeXT runtime, and gcc includes the GNU runtime to be used on platforms other than Mac OS X.”

    Reply

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: