如何使Objective-C項目在Ubuntu上運行?編譯Linux上的Objective-C項目(Ubuntu)
我的文件有:
Fraction.h
#import <Foundation/NSObject.h>
@interface Fraction: NSObject {
int numerator;
int denominator;
}
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(int) numerator;
-(int) denominator;
@end
Fraction.m
#import "Fraction.h"
#import <stdio.h>
@implementation Fraction
-(void) print {
printf("%i/%i", numerator, denominator);
}
-(void) setNumerator: (int) n {
numerator = n;
}
-(void) setDenominator: (int) d {
denominator = d;
}
-(int) denominator {
return denominator;
}
-(int) numerator {
return numerator;
}
@end
的main.m
#import <stdio.h>
#import "Fraction.h"
int main(int argc, const char *argv[]) {
// create a new instance
Fraction *frac = [[Fraction alloc] init];
// set the values
[frac setNumerator: 1];
[frac setDenominator: 3];
// print it
printf("The fraction is: ");
[frac print];
printf("\n");
// free memory
[frac release];
return 0;
}
我已經嘗試了兩種方法來進行編譯:
純GCC:
$ sudo apt-get install gobjc gnustep gnustep-devel $ gcc `gnustep-config --objc-flags` -o main main.m -lobjc -lgnustep-base /tmp/ccIQKhfH.o:(.data.rel+0x0): undefined reference to `__objc_class_name_Fraction'
我創建了一個GNUmakefile的Makefile:
include ${GNUSTEP_MAKEFILES}/common.make TOOL_NAME = main main_OBJC_FILES = main.m include ${GNUSTEP_MAKEFILES}/tool.make
...就跑:
$ source /usr/share/GNUstep/Makefiles/GNUstep.sh $ make Making all for tool main... Linking tool main ... ./obj/main.o:(.data.rel+0x0): undefined reference to `__objc_class_name_Fraction'
因此,在這兩種情況下的編譯器卡在
undefined reference to `__objc_class_name_Fraction'
你有和知道如何解決這個問題?
實際上它我鏈接錯誤。 newacct是對的。 我也不確定包含。我認爲這隻在MacOS上提供,不在GNUStep中。 –
2009-10-13 07:25:53
@Johannes Rudolphp:我將main.m更改爲* .m,它工作正常! – Alex 2009-10-13 10:08:41