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的源碼:

  1. #import <stdio.h>   
  2. int main(int argc,const char *argv[]){   
  3. printf("hello world/n");   
  4. return 0;   
  5. }  
編譯運行:
  1. samsung@coco ~   
  2. $ gcc hello.m   
  3.   
  4. samsung@coco ~   
  5. $ ls   
  6. a.exe  hello.m   
  7.   
  8. samsung@coco ~   
  9. $ ./a.exe   
  10. hello world   
  11.   
  12. samsung@coco ~  

3. 一個更復雜的例子:

代碼:  包含3個文件。
1) Fraction.h:

  1. #import <Foundation/NSObject.h>   
  2.   
  3. @interface Fraction: NSObject {   
  4.     int numerator;   
  5.     int denominator;   
  6. }   
  7.   
  8. -(void) print;   
  9. -(void) setNumerator: (int) d;   
  10. -(void) setDenominator: (int) d;   
  11. -(int) numerator;   
  12. -(int) denominator;   
  13. -(void) setNumerator: (int) n ddd: (int)d;   
  14. -(void) setNumerator: (int)n : (int)d :(int) a;   
  15. // 這裏,有3個setNumerator函數, 是重載。   
  16. @end  

4. 總結:
   1. 用戶也可以使用cygwin+ GNUstep來進行開發;
   2. object-C是以m爲後綴的,用GNU GCC來編譯;
   3. 整個環境和linux差不多;
   4. 初看起來,也很easy了,起碼,環境搭建時,我沒有遇到任何阻礙,呵呵,加油!!!

2)Fraction.m

  1. #import "Fraction.h"  
  2. #import <stdio.h>   
  3.   
  4. @implementation Fraction   
  5. -(void) print {   
  6.     printf( "%i/%i", numerator, denominator );   
  7. }   
  8.   
  9. -(void) setNumerator: (int) n {   
  10.     numerator = n;   
  11. }   
  12.   
  13. -(void) setDenominator: (int) d {   
  14.     denominator = d;   
  15. }   
  16.   
  17. -(int) denominator {   
  18.     return denominator;   
  19. }   
  20.   
  21. -(int) numerator {   
  22.     return numerator;   
  23. }   
  24.   
  25. -(void) setNumerator: (int) n ddd: (int)d {   
  26.     numerator = n;   
  27.     denominator = d;    
  28. }   
  29. -(void) setNumerator: (int)n : (int)d :(int) a {   
  30.         numerator = n;   
  31.         denominator = d;   
  32.         printf("+++++a = %d +++ /n", a);   
  33. }   
  34. @end  

 

3) main.m

  1. #import <stdio.h>   
  2. #import <Foundation/Foundation.h>   
  3. #import "Fraction.h"  
  4.   
  5. int main( int argc, const char *argv[] ) {   
  6.     // create a new instance   
  7.     Fraction *frac = [[Fraction alloc] init];   
  8.       
  9.       
  10.     int x;   
  11.     int y;   
  12.   
  13.     // set the values   
  14.     [frac setNumerator: 1];   
  15.     [frac setDenominator: 3];   
  16.   
  17.     // print it   
  18.     printf( "The fraction is: " );   
  19.   
  20.     [frac print];   
  21.     printf( "/n/n" );   
  22.     NSLog(@"hello world!!!/n");     // ok   
  23.       
  24.     [frac setNumerator:34 ddd: 98];   
  25.       
  26.     [frac print];   
  27.     printf( "/n/n" );   
  28.     NSLog(@"hello world world!!!/n");     // ok   
  29.       
  30.     [frac setNumerator:44 : 55 :66];      // ok   
  31.     [frac print];   
  32.     printf( "/n/n" );   
  33.           
  34.     scanf("%d %d", &x,&y);             //scanf 函數的測試,ok   
  35.       
  36.     [frac setNumerator: x ddd: y];   //ok   
  37.     [frac print];   
  38.   
  39.     // free memory   
  40.     [frac release];   
  41.     // [frac release];         //前面已經release了,所以這裏發生異常:程序崩潰。   
  42.                                //即對空指針進行release,當然不允許了。   
  43.   
  44.     return 0;   
  45. }  

 

編譯方法:
1)將main.m編譯成main.o :
gcc -fconstant-string-class=NSConstantString -c main.m -I /GNUstep/System/Library/Headers

2) 將Fraction.m編譯成Fraction.o :
gcc -c Fraction.m -I /GNUstep/System/Library/Headers

3) 將.o編譯成可執行程序,名爲main(最後生成的是main.exe)
gcc -o main main.o Fraction.o -L /GNUstep/System/Library/Libraries/ -lobjc -lgnustep-base
注意:這時會有warning出現,但可以不用管它,畢竟,我們的可執行程序已經編譯出來了.

運行結果:

  1. samsung@coco ~/objc/fraction   
  2. $ ./main.exe   
  3. The fraction is: 1/3  
  4.   
  5. 2010-08-13 16:29:01.515 main[1212] hello world!!!   
  6. 34/98  
  7.   
  8. 2010-08-13 16:29:01.515 main[1212] hello world world!!!   
  9. +++++a = 66 +++   
  10. 44/55  
  11.   
  12. 22 33  
  13. 22/33  
  14.   
  15. samsung@coco ~/objc/fraction    

 

發佈了12 篇原創文章 · 獲贊 21 · 訪問量 26萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章