我正在尝试运行以下代码:
#import <stdio.h>
#import <Foundation/NSObject.h>
//--------@interface section--------
@interface Fraction: NSObject {
int numerator;
int denominator;
}
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(void) print;
@end
//-------@implementation section-------
@implementation Fraction;
//getters
-(int) numerator {
return numerator;
}
-(int) denominator {
return denominator;
}
//setters
-(void) setNumerator: (int) num {
numerator = num;
}
-(void) setDenominator: (int) denom {
denominator = denom;
}
//other
-(void) print {
printf("The value of the fraction is %i/%i\n", numerator, denominator);
}
@end
//---------program section---------
int main(void) {
Fraction *myFract;
myFract = [Fraction new];
[myFract setNumerator: 1];
[myFract setDenominator: 3];
printf("The numerator is %i, and the denominator is %i\n",
[myFract numerator], [myFract denominator]);
[myFract print];
[myFract free];
return 0;
}
当我编译它
$gcc -framework Foundation p10.m
我得到了。错误:
Program19_2.m: In function ‘main’:
Program19_2.m:55:2: warning: ‘Fraction’ may not respond to ‘+new’ [enabled by default]
Program19_2.m:55:2: warning: (Messages without a matching method signature [enabled by
default]
Program19_2.m:55:2: warning: will be assumed to return ‘id’ and accept [enabled by
default]
Program19_2.m:55:2: warning: ‘...’ as arguments.) [enabled by default]
Program19_2.m:64:2: warning: ‘Fraction’ may not respond to ‘-free’ [enabled by default]
/tmp/ccVRbEBu.o: In function `main':
Program19_2.m:(.text+0x6d): undefined reference to `objc_get_class'
Program19_2.m:(.text+0x7f): undefined reference to `objc_msg_lookup'
Program19_2.m:(.text+0xa4): undefined reference to `objc_msg_lookup'
Program19_2.m:(.text+0xcd): undefined reference to `objc_msg_lookup'
Program19_2.m:(.text+0xf6): undefined reference to `objc_msg_lookup'
Program19_2.m:(.text+0x119): undefined reference to `objc_msg_lookup'
/tmp/ccVRbEBu.o:Program19_2.m:(.text+0x14e): more undefined references to
`objc_msg_lookup' follow
/tmp/ccVRbEBu.o: In function `__objc_gnu_init':
Program19_2.m:(.text+0x19a): undefined reference to `__objc_exec_class'
/tmp/ccVRbEBu.o:(.data+0x148): undefined reference to `__objc_class_name_Object'
collect2: error: ld returned 1 exit status
我只是从教科书中复制代码。更改我导入的库,因为它们现在已弃用 (Object.h)。我想知道为什么它不运行。我编译程序的方式有问题吗?