nvteighen
February 8th, 2009, 07:43 PM
Hi there!
I'm here looking a bit into Objective-C and I'm impressed. It's a really nice language (just "C with Classes" not C++ bloat) but I won't go into that here.
But I had real troubles to get the sources compiled with gcc under Debian (so Ubuntu is surely the same) and after a lot of research, I could. Here's a little how-to for anyone interested.
1. Install the GNU Objective-C Runtime.
sudo apt-get install gobjc
Despite what some webpages imply, you don't need GNUStep (the GNU clone of Apple NeXTStep).
2. Write some source. Here's a very small example.
// hello.m
#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;
}
As you see, there are some differences with Apple Objective-C. The base object class is called Object, not NSObject. The Object class's interface is at objc/Object.h, as you see it #import'ed.
3. Compile:
gcc -o hello hello.m -Wall -lobjc
-o and -Wall are optional, as you surely know if you have used gcc or g++ before for C or C++, respectively. The key argument is -lobjc. For some weird reason, you have to manually link to the GNU Objective-C Runtime, otherwise, you're lost.
If you happen to be using gcc in Mac OS X, then you have to also pass -fgnu-runtime to avoid the Apple NeXTStep Runtime.
@end ;)
I'm here looking a bit into Objective-C and I'm impressed. It's a really nice language (just "C with Classes" not C++ bloat) but I won't go into that here.
But I had real troubles to get the sources compiled with gcc under Debian (so Ubuntu is surely the same) and after a lot of research, I could. Here's a little how-to for anyone interested.
1. Install the GNU Objective-C Runtime.
sudo apt-get install gobjc
Despite what some webpages imply, you don't need GNUStep (the GNU clone of Apple NeXTStep).
2. Write some source. Here's a very small example.
// hello.m
#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;
}
As you see, there are some differences with Apple Objective-C. The base object class is called Object, not NSObject. The Object class's interface is at objc/Object.h, as you see it #import'ed.
3. Compile:
gcc -o hello hello.m -Wall -lobjc
-o and -Wall are optional, as you surely know if you have used gcc or g++ before for C or C++, respectively. The key argument is -lobjc. For some weird reason, you have to manually link to the GNU Objective-C Runtime, otherwise, you're lost.
If you happen to be using gcc in Mac OS X, then you have to also pass -fgnu-runtime to avoid the Apple NeXTStep Runtime.
@end ;)