当我尝试编译这段代码,我得到了形式here用gcc:如何用GCC编译Objective-C代码?
#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;
}
与COMAND行: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
我做错了什么?
env:linux-x86_64,gcc-4.7.2
谢谢。
它应该是'-x objective-c'而不是'-x objective-C++'吗? – KevinDTimm
@KevinDTimm,同样的错误。 – niXman