開發環境實際上就是「編譯器+庫+頭文件+輔助開發工具」。linux
在linux上,首先分別安裝object-c相關的編譯器,庫,輔助開發工具和頭文件。c++
(類別:包名)
ubuntu
編譯器:gobjc,gobjc++工具
庫:gnustep開發工具
輔助開發工具:gnustep-makecode
頭文件:gnustep-devel開發
(以上包名是ubuntu上的,其餘linux發行版本的名字可能不會徹底同樣,但關鍵詞確定是相同的,好比gobjc,gnustep等)編譯器
代碼示例:it
Fraction.h
io
#import <Foundation/NSObject.h> @interface Fraction: NSObject { int numerator; int denominator; } -(void) print; -(void) setNumerator: (int) d; -(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[]) { Fraction *frac = [[Fraction alloc] init]; [frac setNumerator: 1]; [frac setDenominator: 3]; printf("The fraction is: "); [frac print]; printf("\n"); [frac release]; return 0; }
Makefile示例:
Fraction:
gcc Fraction.m main.m `gnustep-config --objc-flags` -lgnustep-base `gnustep-config --objc-libs` -Wl,--no-as-needed -o Fraction
運行結果:
chenqi@pek-qchen1-d1:~/projects/OBJ-C [1] $ ./Fraction The fraction is: 1/3chenqi@pek-qchen1-d1:~/projects/OBJ-C [1] $