日期:2014-05-17  浏览次数:20699 次

(转)Windows如何进行Object-C的开发

Windows下Object-C编译环境的搭建:
1. 下载并安装以下两个软件 :
??? GNUstep System(我用的版本是:gnustep-msys-system-0.25.1-setup.exe)
??? GNUstep Core(我用的版本是:gnustep-core-0.25.0-setup.exe)

??? 下载地址: http://www.gnustep.org/experience/Windows.html


??? 安装方法:直接点击上exe文件即可,另外,最好选择默认安装路径:C:/GNUstep。


2. 测试:
?? 安装完成后,进入"开始-程序-GNUstep-Shell",出现的窗口就是shell 窗口,就可以进行编辑(vi/vim)和编译(gcc) object-C代码了。
?? 这个shell的默认路径是 /home/<username>,例如,我的是 /home/samsung/。
?? 另外,我直接用devc++,UE等编辑软件编辑程序,放在/home/samsung/下,进行编译和运行。
?? 下面是我的代码和运行结果:
??? hello.m的源码:

?

#import <stdio.h>    
int main(int argc,const char *argv[]){    
printf("hello world/n");    
return 0;    
}   
?编译运行:?
samsung@coco ~    
$ gcc hello.m    
   
samsung@coco ~    
$ ls    
a.exe  hello.m    
   
samsung@coco ~    
$ ./a.exe    
hello world    
   
samsung@coco ~  

?3. 一个更复杂的例子:?


代码:? 包含3个文件。
1) Fraction.h:

?

#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;    
-(void) setNumerator: (int) n ddd: (int)d;    
-(void) setNumerator: (int)n : (int)d :(int) a;    
// 这里,有3个setNumerator函数, 是重载。    
@end   

?

4. 总结:
?? 1. 用户也可以使用cygwin+ GNUstep来进行开发;
?? 2. object-C是以m为后缀的,用GNU GCC来编译;
?? 3. 整个环境和linux差不多;
?? 4. 初看起来,也很easy了,起码,环境搭建时,我没有遇到任何阻碍,呵呵,加油!!!

?

?

2)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;    
}    
   
-(void) setNumerator: (int) n ddd: (int)d {    
    numerator = n;    
    denominator = d;     
}    
-(void) setNumerator: (int)n : (int)d :(int) a {    
        numerator = n;    
        denominator = d;    
        printf("+++++a = %d +++ /n", a);    
}    
@end   

?

3) main.m?

#import <stdio.h>    
#import <Foundation/Foundation.h>    
#import "Fraction.h"   
   
int main( int argc, const char *argv[] ) {    
    // create a new instance    
    Fraction *frac = [[Fraction alloc] init];    
       
       
    int x;    
    int y;    
   
    // set the values    
    [frac setNumerator: 1];    
    [frac setDenominator: 3];    
   
    // print it    
    printf( "The fraction is: " );    
   
    [frac print];    
    printf( "/n/n" );    
    NSLog(@"hello world!!!/n");     // ok    
       
    [frac setNumerator:34 ddd: 98];    
       
    [frac print];    
    printf( "/n/n" );    
    NSLog(@"hello world world!!!/n");     // ok    
       
    [frac setNumerator:44 : 55 :66];      // ok    
    [frac print];    
    printf( "/n/n" );    
           
    scanf("%d %d", &x,&y);             //scanf 函数的测试,ok    
       
    [frac setNumerator: x ddd: y];   //ok    
    [frac print];    
   
    // free memory    
    [frac release];    
    // [frac release];         //前面已经release了,所以这里发生异常:程序崩溃。    
                               //即对空指针进行release,当然不允许了。    
   
    return 0;    
}   

?

编译方法:
1)将main.m编译成mai