ubuntu 12.04.3:在objective-c中使用gcc 4.8.2

时间:2014-01-23 05:45:40

标签: objective-c ubuntu gcc

我在ubuntu.12.04.3中使用了源编译(--enable-language = c,c ++,objc,obj-c ++)gcc-4.8.2。

我写了这个测试代码。

#import <objc/Object.h>

#include <stdio.h>

@interface Number : Object
{
@public
    int number;
}

- (void)printNum;

@end

@implementation Number : Object

- (void)printNum
{
    printf("%d\n", number);
}

@end

int main()
{
    Number *num = [[Number alloc] init];
    num->number = 7;
    [num printNum];
    return 0;
}

但我收到以下错误:

test.m: In function ‘main’:
test.m:26:2: warning: ‘Number’ may not respond to ‘+alloc’ [enabled by default]
  Number *num = [[Number alloc] init];
  ^
test.m:26:2: warning: (Messages without a matching method signature [enabled by default]
test.m:26:2: warning: will be assumed to return ‘id’ and accept [enabled by default]
test.m:26:2: warning: ‘...’ as arguments.) [enabled by default]
test.m:26:2: warning: no ‘-init’ method found [enabled by default]

为什么会出现这些警告?

并且,我尝试安装libobjc 4.8.2,但我找不到源代码。所以我做“sudo apt-get install gobjc”。和“sudo apt-get install libobjc4”。但是当程序启动时,它会发生分段错误;

我在哪里可以下载libobjc的源代码(和另一个必需的运行时库)?

1 个答案:

答案 0 :(得分:1)

Object类已过时,并未实现alloc方法,因此出现错误消息。

请改用NSObject。您必须包含适当的框架,即Foundation。