我正在尝试运行以下代码:
#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
我得到了ff。错误:
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)现已弃用。我想知道它为什么不运行。我编译程序的方式有问题吗?
答案 0 :(得分:2)
你遇到的问题是你的书早于现代Objective-C(即Objective-C 2.0)。
Object
课程已存在数十年,但自从推出NeXTSTEP框架以来,它已经过时了。在没有NeXTSTEP框架的情况下编写Objective-C是非常罕见的(无论是Apple的基础还是GNU的GNUstep)。
忘记了解Object
。虽然您的书可能能够教您一些Objective-C的基础知识,但它并不能教您如何真正改变您使用它编程的语言(包括块,属性等)的任何语言最新进展。
答案 1 :(得分:1)
如果gcc找不到Object.h,它会给出一个错误指示。问题是Apple在Objective-C 2.0中从Object.h
(或至少是它的接口)中删除了大部分方法。
<强> The main reason your program is giving these errors is that gcc is not including the Object class.
强>
相反,您应该继承NSObject并包含Foundation框架。
答案 2 :(得分:0)
使用alloc
然后init
进行初始化,以释放内存使用release
另请注意声明为
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
实施为
// setter方法
-(void) setNumerator: (int) num
-(void) setDenominator: (int) denom
使用
-(void) setNumerator: (int) n {
numerator = num;
}
-(void) setDenominator: (int) d {
denominator = denom;
}
答案 3 :(得分:0)
Program19_2.m:55:2: warning: ‘Fraction’ may not respond to ‘+new’ [enabled by default]
我不明白这个错误。我只看了/usr/include/objc/Object.h
,界面肯定有new
。唯一奇怪的是,如果你使用的是Objective-C 2.0 Object
#define
,那么你应该得到一个接口根本不存在的错误。也许其他一些标题的前向声明为Object
。尝试使用-E
标志进行编译,以查看预处理的结果。
am19_2.m:64:2: warning: ‘Fraction’ may not respond to ‘-free’ [enabled by default]
这是由与第一个错误相同的问题引起的。
Program19_2.m:(.text+0x6d): undefined reference to `objc_get_class'
此表单的所有错误都是由于您没有将程序与Objective-C运行时链接引起的。你可以用这种方式摆脱它们:
gcc p10.m -lobjc
以上都很好,但真正的问题是你的教科书已经过时了很多年。我在十多年前开始使用Objective-C,并且没有人使用Object
作为基类。您需要找到一本使用Foundation和Cocoa中的类的书 - 开始NS
的那些。
答案 4 :(得分:0)
一个好的开始是安装clang ..应该至少3.2(我使用trunk,因为它似乎与ObjC ++的问题较少),使它使用GNUstep又名GNUstep运行时附带的libobjc,而不是GCC。如果您不需要其他任何GCC,请卸载GCC和随附的任何libobjc,只需使用clang即可。使用GNUstep + GNUmakefile构建项目是一个好主意,有一些教程你可以google。