Windows下Objective-C環境搭建教程

來源:互聯網
上載者:User

WindowsObjective-C環境搭建教程是本文要介紹的內容,主要是在windows平台下搭建Objective-C開發環境,具體內容來看本文詳解。

1.安裝4個檔案,直接找我要吧

2、測試:

安裝完成後,進入"開始-程式-GNUstep-Shell",出現的視窗就是shell 視窗,就可以進行編輯vi/vim)和編譯gcc) object-C代碼了。

這個shell的預設路徑是 \home\<username>,例如,我的是 /home/samsung/。

另外,我直接用devc++,UE等編輯軟體編輯程式,放在/home/samsung/下,進行編譯和運行。

下面是My Code和運行結果:

hello.m的源碼:

 
  1. #import <stdio.h> 
  2. int main(int argc,const char *argv[]){  
  3. printf("hello world\n");  
  4. return 0;  

編譯運行:

 
  1. samsung@coco ~  
  2. $ gcc hello.m  
  3. samsung@coco ~  
  4. $ ls  
  5. a.exe  hello.m  
  6. samsung@coco ~  
  7. $ ./a.exe  
  8. hello world  
  9. 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 

(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.     nnumerator = n;  
  11. }  
  12.  
  13. -(void) setDenominator: (int) d {  
  14.     ddenominator = 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.     nnumerator = n;  
  27.     ddenominator = d;   
  28. }  
  29. -(void) setNumerator: (int)n : (int)d :(int) a {  
  30.         nnumerator = n;  
  31.         ddenominator = 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.     // free memory  
  39.     [frac release];  
  40.     // [frac release];         //前面已經release了,所以這裡發生異常:程式崩潰。  
  41.                                //即對null 指標進行release,當然不允許了。  
  42.     return 0;  

編譯方法:

(1)將main.m編譯成main.o :

 
  1. gcc -fconstant-string-class=NSConstantString -c main.m -I /GNUstep/System/Library/Headers 

2) 將Fraction.m編譯成Fraction.o :

 
  1. gcc -c Fraction.m -I /GNUstep/System/Library/Headers 

3) 將.o編譯成可執行程式,名為main(最後產生的是main.exe)

 
  1. 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    

4、總結:

1、使用者也可以使用cygwin+ GNUstep來進行開發;

2、Objective-C是以m為尾碼的,用GNU GCC來編譯;

3、整個環境和linux差不多;

小結:WindowsObjective-C環境搭建教程的內容介紹完了,希望通過本文的學習能對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.