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?