I have these files:
Project.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;
}
Fraction.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
Fraction.h
#import <Foundation/Foundation.h>
@interface Fraction: NSObject {
int numerator, denominator;
}
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(void) print;
-(double) value;
@end
Arranged in a file like so:
Objective-C
|_Fraction.h
|_Fraction.m
|_Project.m
However, when I run this (I am using GNUstep) I get this error:
undefined reference to '__objc_class_name_Fraction'
This error is telling you that
Fraction.mwas not compiled or linked, and therefore when you build the project, theFractionclass was not found.You'll get this error if you neglect to add
Fraction.mto yourGNUmakefile.You probably have a
GNUmakefilethat looks like:You want to change that to include
Fraction.min theOBJC_FILESline:Clearly, your
makefilecould be different, but you get the idea. Make sure to includeFraction.min your list of source files.