LazySloth13
LazySloth13

Reputation: 2487

undefined reference to '__objc_class_name_Fraction'

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'

Upvotes: 3

Views: 1491

Answers (1)

Rob
Rob

Reputation: 437381

This error is telling you that Fraction.m was not compiled or linked, and therefore when you build the project, the Fraction class was not found.

You'll get this error if you neglect to add Fraction.m to your GNUmakefile.

You probably have a GNUmakefile that looks like:

include $(GNUSTEP_MAKEFILES)/common.make

TOOL_NAME = MyProject
MyProject_OBJC_FILES = Project.m

include $(GNUSTEP_MAKEFILES)/tool.make

You want to change that to include Fraction.m in the OBJC_FILES line:

include $(GNUSTEP_MAKEFILES)/common.make

TOOL_NAME = MyProject
MyProject_OBJC_FILES = Project.m Fraction.m

include $(GNUSTEP_MAKEFILES)/tool.make

Clearly, your makefile could be different, but you get the idea. Make sure to include Fraction.m in your list of source files.

Upvotes: 3

Related Questions