I am trying to learn objective c on windows. My program compiles with warnings
My code is
#include <objc/Object.h>
@interface Greeter:Object
{
/* This is left empty on purpose:
** Normally instance variables would be declared here,
** but these are not used in our example.
*/
}
- (void)greet;
@end
#include <stdio.h>
@implementation Greeter
- (void)greet
{
printf("Hello, World!\n");
}
@end
#include <stdlib.h>
int main(void)
{
id myGreeter;
myGreeter=[[Greeter alloc] init];
[myGreeter greet];
[myGreeter release];
return EXIT_SUCCESS;
}
I compile my program on GNUStep using the following command
gcc -o Greeter Greeter.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Libra
/Libraries -lobjc -lgnustep-base -fconstant-string-class=NSConstantString
I get the following warnings on compilation
: 'Greeter' may not respond to '+alloc' [enabled by default]
: (Messages without a matching method signature [enabled by default]
: will be assumed to return 'id' and accept [enabled by default]
: '...' as arguments.) [enabled by default]
: no '-init' method found [enabled by default]
: no '-release' method found [enabled by default]
And so when I run my executable the object does not get instantiated.
I am using gcc from MinGW where gcc version is 4.6.2
–UPDATE—
The program runs fine when I extend from NSObject instead of Object
–UPDATE 2 —-
My Object.h looks like
#include <objc/runtime.h>
@interface Object
{
Class isa;
}
@end
–UPDATE 3 —-
I have modified my code as follows. It compiles fine, but I am not sure if this is the right way to go about things
@interface Greeter
{
/* This is left empty on purpose:
** Normally instance variables would be declared here,
** but these are not used in our example.
*/
}
- (void)greet;
+ (id)alloc;
- (id)init;
- release;
@end
#include <stdio.h>
@implementation Greeter
- (void)greet
{
printf("Hello, World!\n");
}
+ (id)alloc
{
printf("Object created");
return self;
}
- (id)init
{
printf("Object instantiated");
return self;
}
- release {}
@end
#include <stdlib.h>
int main(void)
{
id myGreeter;
myGreeter=[[Greeter alloc] init];
[myGreeter greet];
[myGreeter release];
return EXIT_SUCCESS;
}
From memory, the
Objectclass does not implement retain counts, so it wouldn’t haverelease, it’ll havefreeor some other method. It should have+allocand-initthough. Since there’s no “Objective-C standard”, you’ll have to open up yourobjc/Object.hand see exactly what it offers.Note that on GCC 4.6.2,
objc/Object.hactually includesobjc/deprecated/Object.h, meaning support forObjectas a class may be fairly limited. If it doesn’t include it, try including it yourself: