在Windows上编写Object C程序

来源:互联网 发布:淘宝管控记录 编辑:程序博客网 时间:2024/04/28 03:16

随着iphone的流行,越来越多的程序员开始学习Object C编程,但一般来说编写Object C需要iMac计算机。这对于刚开始学习Object C编程的程序员是个问题。
本文给出了一个在 windows 平台上学习Object C编程的方法。
1.下载GNUStep
   http://ftpmain.gnustep.org/pub/gnustep/binaries/windows/
   下载
       gnustep-msys-system-x.x.x-setup.exe
       gnustep-core-x.x.x-setup.exe
       gnustep-cairo-x.x.x-setup.exe
       gnustep-devel-x.x.x-setup.exe
  将下载的GNUStep安装,比如C:\GNUStep
2. 下载JEdit
    http://www.jedit.org/index.php?page=download
    JEdit 是Freeware,可以用来编辑 .m 文件 .m 是Object C缺省后缀。 .m 相当于 .c 文件
3. 一个Object C教材
    http://www.otierney.net/objective-c.html
---------------------------------------------
4. 安装后,执行msys.bat 启动 GNUStep 环境 (类Linux环境)

5. 编写示例程序
fraction.h

#import <Foundation/NSObject.h> @interface Fraction: NSObject {     int numerator;     int denominator; } -(void) print; -(void) setNumerator: (int) n; -(void) setDenominator: (int) d; -(int) numerator; -(int) denominator; @end

fraction.m

#import "fraction.h" #import @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; } @end


main.m

#import #import "fraction.h" int main( int argc, const char *argv[] ) {     // create a new instance     Fraction *frac = [[Fraction alloc] init];     // set the values     [frac setNumerator: 1];     [frac setDenominator: 3];     // print it     printf( "The fraction is: " );     [frac print];     printf( "\n" );     // free memory     [frac release];     return 0; }


6. 编写Makefile
   在当前目录下创建GNUmakefile

include $(GNUSTEP_MAKEFILES)/common.make TOOL_NAME = Hello Hello_OBJC_FILES = main.m fraction.m include $(GNUSTEP_MAKEFILES)/tool.make

6. 编译程序
   $ make
  将创建 obj目录 运行  hello.exe
   The fraction is: 1/3
这样环境就搭好了,你就可以继续学习 Object C了

最终写iphone程序一般还是要Mac OS.

原创粉丝点击