0

I'm trying to get into objective c, but I don't have a mac so I am trying to get it to work on linux. I found these two articles which talk about compiling objective c on linux: this one and this one

Ok, I forgot to say that I don't want to use gnustep, it seems dead and I don't want all the cocoa framework, just the objective c syntax and the c standard library. But I can't compile any code without gnustep!

If I try to compile this code:

#import <objc/Object.h>
#import <stdio.h>

@interface Number: Object
{
@public
    int number;

}

- (void)printNum;

@end

@implementation Number: Object


- (void)printNum
{
    printf("%d\n", number);
}

@end

int main(void)
{
    Number *myNumber = [Number new]; // equal to [[Number alloc] init]

    myNumber->number = 6;

    [myNumber printNum];

    return 0;
}

I get a segmentation fault, because there's no init nor alloc methods. And if I don't inherit from Object, like so:

#include <stdio.h> // C standard IO library (for printf)
#include <stdlib.h> // C standard library
// Interface
@interface test
 -(void)sayHello :(char *)message;
@end

// Implementation
@implementation test
 -(void)sayHello :(char *)message {
  printf("%s", message);
 }

int main(int argc, char *argv[]) {
 test *test = [[test alloc] init];
 [test sayHello:"Hello world"];
}

I get a Bus error. It seems like the only way to create interfaces and implement them is inheriting from NSObject. How can I fix this?

By the way, I'm using gcc with -lobjc flag (with gobjc)

EDIT: ok, so I have to create a root object myself if I don't want to use a framework. How can I do this? I imagine it's something like malloc and free in the init and release methods, but I'm not sure. How can I do this?

1 Answer 1

1

That's not really how Objective C works. You need the runtime, I'm afraid. Many of the good bits of Objective C are about what happens at run- rather than compile-time. If you take away the library, there's not a lot left.

Sign up to request clarification or add additional context in comments.

5 Comments

But, isn't gobjc and libobjc enough then? Libobjc is the runtime library for objective c. Then I guess i'll be using gnustep. (Is it still maintained anyways?)
@ochi12: You can just use the Objective-C runtime libraries, but then you'll have to recreate a root object with methods like alloc and init yourself.
They're pretty tightly coupled, to the point where it's difficult to say what's where (unless you're the maintainer, in which case I'm sure it makes some kind of sense). I'm sure @mipadi is right, but the path of least resistance is certainly just to use GNUStep.
Looks like commits are still being made to the repository, though I'm not clear how active it actually is.
So if I implement the NSObject class then I get objective c without the foundation framework? That would be awesome, are there any resources on how to do it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.