当我尝试编译这段代码时,我用 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;
}
使用命令行:g++ -x objective-c++ test.mm -otest -lobjc
编译时我收到以下警告:
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]
如果我尝试运行 - 我会得到 SIGSEGV:
$> ./test
$> Segmentation fault
我做错了什么?
环境:linux-x86_64,gcc-4.7.2
谢谢。