3

我有这些文件:

项目.m

#import "Fraction.h"

int main(int argc, char *argv[]) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    Fraction *fraction = [[Fraction alloc] init];
    [fraction setNumerator: 2];
    [fraction setDenominator: 3];
    [fraction print];
    NSLog(@"%g", [fraction value]);
    [fraction release];
    [pool drain];
    return 0;
}

分数.m

#import "Fraction.h"

@implementation Fraction
-(void) setNumerator: (int) n {
    numerator = n;
}

-(void) setDenominator: (int) d {
    denominator = d;
}

-(void) print {
    NSLog(@"%i/%i\n", numerator, denominator);
}

-(double) value {
    return (double) numerator / denominator;
}
@end

分数.h

#import <Foundation/Foundation.h>

@interface Fraction: NSObject {
    int numerator, denominator;
}

-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(void) print;
-(double) value;
@end

像这样安排在一个文件中:

Objective-C
|_Fraction.h
|_Fraction.m
|_Project.m

但是,当我运行这个(我正在使用GNUstep)时,我收到了这个错误:

undefined reference to '__objc_class_name_Fraction'
4

1 回答 1

3

此错误告诉您Fraction.m未编译或链接,因此当您构建项目时,Fraction找不到该类。

如果您忽略添加Fraction.m到您的GNUmakefile.

你可能有一个GNUmakefile看起来像:

include $(GNUSTEP_MAKEFILES)/common.make

TOOL_NAME = MyProject
MyProject_OBJC_FILES = Project.m

include $(GNUSTEP_MAKEFILES)/tool.make

您想将其更改为包含Fraction.m在该OBJC_FILES行中:

include $(GNUSTEP_MAKEFILES)/common.make

TOOL_NAME = MyProject
MyProject_OBJC_FILES = Project.m Fraction.m

include $(GNUSTEP_MAKEFILES)/tool.make

显然,您makefile可能会有所不同,但您明白了。确保包含Fraction.m在您的源文件列表中。

于 2013-08-09T16:20:42.167 回答