4

When I try to compile this code that I got form here with gcc:

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

@interface Hello: Object
- (void) init;
- (void) say;
@end

@implementation Hello
- (void) init {
  [super init];
}
- (void) say {
  printf("Hello, world!\n");
}
@end

int main() {
  Hello *hello = [Hello new];
  [hello say];
  [hello free];
  return 0;
}

with comand line: g++ -x objective-c++ test.mm -otest -lobjc

When compiling I get the following warnings:

test.mm:11:14: warning: ‘Object’ may not respond to ‘-init’ [enabled by default]
test.mm:11:14: warning: (Messages without a matching method signature
[enabled by default]
test.mm:11:14: warning: will be assumed to return ‘id’ and accept
[enabled by default]
test.mm:11:14: warning: ‘...’ as arguments.) [enabled by default]
test.mm: In function ‘int main()’:
test.mm:19:28: warning: ‘Hello’ may not respond to ‘+new’ [enabled by default]
test.mm:21:14: warning: ‘Hello’ may not respond to ‘-free’ [enabled by default]

and if I try to run - I get SIGSEGV:

$> ./test
$> Segmentation fault

What I am doing wrongly?

env: linux-x86_64, gcc-4.7.2

Thanks.

2
  • should it be -x objective-c instead of -x objective-c++? Commented Jan 18, 2013 at 23:51
  • @KevinDTimm, the same error. Commented Jan 19, 2013 at 0:25

1 Answer 1

3

That code is wrong. And ancient. It would never have worked. Now it would have (fixed and added some additional examples).

First, check to see if you have GNUStep installed. If you do, switch to using NSObject and libFoundation (actually libgnustep-base, as Fred points out in the comments).

Secondly, [IIRC -- scratching old brain cells here] that init still returned (id) when Object was still the root class. Since you aren't doing anything useful in that init method, just delete it entirely.

The compiler errors indicate that your Hello subclass is not correctly inheriting from Object, which makes no sense.

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

4 Comments

@bumm, Wikipedia again lying. Thank you.
@niXman it is not lying, it is just not for cut and pasting. Allow me to correct bbum's comment for niXman, in GNUstep it's libgnustep-base, libFoundation actually exists as a different project and was at some points or may be once interchangeable with libgnustep-base.
Thanks for the correction -- it has been more than a decade since I compiled ObjC on Linux. In fact, the Wikipedia article was lying until I fixed it. The code didn't work and neve would have.
Ah, I thought he meant the Objective-C article.

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.