命令行编译OC源文件

在学习Objective-C的时候,每次就为了那么几行代码就打开XCode简历工程,似乎先得太小题大作了。通常直接用VIM在命令行敲下来,编译运行一下就OK。

#import <Foundation/Foundation.h>

@interface MyPoint: NSObject
@property float x;
@property float y;
-(float) distance:(MyPoint*)other;
-(NSString*) description;
@end

@implementation MyPoint
@synthesize x,y;
-(float) distance:(MyPoint*)other{
	return sqrt(((self.x-other.x)*(self.x-other.x))+
	((self.y-other.y)*(self.y-other.y)));
}
-(NSString*) description{
	return [NSString stringWithFormat:@"(%f,%f)",self.x,self.y];
}
@end

int main(int argc,char *argv[]){
	MyPoint *a=[[MyPoint alloc] init];
	MyPoint *b=[[MyPoint alloc] init];
	a.x=10.0f;
	a.y=10.0f;
	b.x=20.0f;
	b.y=20.0f;
	NSLog(@"a %@",a);
	NSLog(@"b %@",b);
	NSLog(@"Distance is %f",[a distance:b]);
	return 0;
}

编译命令:

clang -fobjc-arc point.m -o point -framework Foundation

运行:

./point

 

标签: , , , ,
文章分类 iPhone应用开发

发表评论