Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'
like image 395
LazySloth13 Avatar asked Nov 28 '25 17:11

LazySloth13


1 Answers

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.

like image 75
Rob Avatar answered Dec 02 '25 03:12

Rob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!