Windows如何進行Object-C的開發

來源:互聯網
上載者:User

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/下,進行編譯和運行。
   下面是My Code和運行結果:
    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

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了,所以這裡發生異常:程式崩潰。
                               //即對null 指標進行release,當然不允許了。

    return 0;
}

編譯方法:
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出現,但可以不用管它,畢竟,我們的可執行程式已經編譯出來了.

運行結果:
samsung@coco ~/objc/fraction
$ ./main.exe
The fraction is: 1/3

2010-08-13 16:29:01.515 main[1212] hello world!!!
34/98

2010-08-13 16:29:01.515 main[1212] hello world world!!!
+++++a = 66 +++
44/55

22 33
22/33

samsung@coco ~/objc/fraction   

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

5.補充:

當然,你也可以使用 gcc inputfile.m -o output來指定輸出的檔案名稱。
   
    
    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.