当前位置: 首页 > news >正文

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;  
编译运行:
  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;  

编译方法:
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 

相关文章:

  • 简单的GCC语法: 弄清gcc test.c 与 gcc -c test.c 的差别
  • Windows下UltraEdit查看Objective-C代码高亮工具
  • 用ultraEdit打造自己的Objective-C IDE for Windows
  • 在项目中将数据导出为Excel格式时出现“检索COM 类工厂中CLSID 为 {00024500-0000-0000-C000-000000000046}的组件时失败
  • ExtJS Grid 改变单元格背景颜色的方法
  • Connector/Net no longer supports server versions prior to 5.0
  • The current identity (JSTAM2\jstcrm) does not have write access to 'C:\WINDOWS\Microsoft.NET\Framewo
  • smtp服务器无法绑定实例1
  • 无法对 数据库'UDS' 执行 删除,因为它正用于复制
  • SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/OpenDatasource' 的访问
  • virtualbox安装增强功能:鼠标在主机与虚拟机之间自由切换
  • Linux 图形界面与命令行模式切换
  • MySql四舍五入
  • linux下用rpm 安装jdk
  • linux安装gcc和c++库
  • CAP理论的例子讲解
  • CentOS 7 修改主机名
  • JavaScript 无符号位移运算符 三个大于号 的使用方法
  • java小心机(3)| 浅析finalize()
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • ViewService——一种保证客户端与服务端同步的方法
  • 阿里研究院入选中国企业智库系统影响力榜
  • 解析带emoji和链接的聊天系统消息
  • 推荐一款sublime text 3 支持JSX和es201x 代码格式化的插件
  • 微信支付JSAPI,实测!终极方案
  • 携程小程序初体验
  • 学习使用ExpressJS 4.0中的新Router
  • 通过调用文摘列表API获取文摘
  • 曾刷新两项世界纪录,腾讯优图人脸检测算法 DSFD 正式开源 ...
  • ​​​​​​​​​​​​​​汽车网络信息安全分析方法论
  • ​Base64转换成图片,android studio build乱码,找不到okio.ByteString接腾讯人脸识别
  • ​中南建设2022年半年报“韧”字当头,经营性现金流持续为正​
  • #我与Java虚拟机的故事#连载16:打开Java世界大门的钥匙
  • (3)llvm ir转换过程
  • (31)对象的克隆
  • (delphi11最新学习资料) Object Pascal 学习笔记---第8章第5节(封闭类和Final方法)
  • (Matalb回归预测)PSO-BP粒子群算法优化BP神经网络的多维回归预测
  • (超详细)2-YOLOV5改进-添加SimAM注意力机制
  • (免费分享)基于springboot,vue疗养中心管理系统
  • (区间dp) (经典例题) 石子合并
  • (转)linux自定义开机启动服务和chkconfig使用方法
  • ***监测系统的构建(chkrootkit )
  • .MyFile@waifu.club.wis.mkp勒索病毒数据怎么处理|数据解密恢复
  • .net core webapi Startup 注入ConfigurePrimaryHttpMessageHandler
  • .NET Micro Framework 4.2 beta 源码探析
  • .net 发送邮件
  • .net反编译工具
  • .NET轻量级ORM组件Dapper葵花宝典
  • /proc/vmstat 详解
  • @Bean注解详解
  • @EnableConfigurationProperties注解使用
  • [AX]AX2012 SSRS报表Drill through action
  • [BZOJ3211]:花神游历各国(小清新线段树)
  • [C++]priority_queue的介绍及模拟实现
  • [CDOJ 1343] 卿学姐失恋了