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

Windows下Object-C编译环境的搭建
Windows下Object-C编译环境的搭建其实就是搭建一个类似于linux的Cygwin和MinGW的编译环境;

其实Cygwin和MinGW两者相比较我还是比较喜欢Cygwin的环境(更贴近于linux),下面我们就直接说怎么编译吧!
谢谢大家贡献的资源!
下面是我收集的仅限学习使用:


例子1:

hello.m的源码:
                 
   
 #import <stdio.h>
    int main(int argc,const char *argv[]){
    printf("hello world/n");
    return 0;
    }

   
    编译运行:

yaoming168@coco ~
$ gcc hello.m

yaoming168@coco ~
$ ls
a.exe  hello.m

yaoming168@coco ~
$ ./a.exe
hello world

yaoming168@coco ~

例子2:

代码:  包含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了,所以这里发生异常:程序崩溃。 
                               //即对空指针进行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出现,但可以不用管它,毕竟,我们的可执行程序已经编译出来了.

运行结果:
yaoming168@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