Windows下Objective-C环境搭建教程

移动开发 iOS
Windows下Objective-C环境搭建教程是本文要介绍的内容,主要是在windows平台下搭建Objective-C开发环境,具体内容来看本文详解。

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/下,进行编译和运行。

下面是我的代码和运行结果:

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.                                //即对空指针进行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环境搭建教程的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 新浪博客
相关推荐

2011-08-17 11:28:54

Objective-C模拟开发Windows平台

2011-07-08 13:49:46

Objective-C UUID

2011-07-27 17:41:35

Objective-C Xcode

2011-07-27 17:10:30

Objective-C 持久化

2011-08-05 14:03:39

Objective-C 对象 模板

2011-08-16 10:23:04

Objective-CNSAutoreleaXcode常用键

2011-07-29 16:08:31

Objective-C 内存

2011-08-04 15:55:50

Windows 编译 Objective-

2011-08-17 10:00:12

Objective-CProperty

2011-07-21 09:42:27

Objective-C 内存 Autoreleas

2011-05-11 15:58:34

Objective-C

2013-03-27 12:54:00

iOS开发Objective-C

2011-05-11 11:20:26

Objective-C

2013-06-20 10:40:32

Objective-C实现截图

2011-08-17 09:55:45

Objective-CCategory

2011-07-19 15:15:09

Objective-C 内存

2011-07-25 17:31:49

iPhone Objective-

2011-08-10 18:07:29

Objective-C反射

2013-04-11 14:32:00

Objective-CiOS开发内存管理@synthesize

2012-06-15 09:47:48

Objective-CCategory
点赞
收藏

51CTO技术栈公众号