Objective-C现在几乎已经变成了苹果的专利了,可以直接在苹果的Xcode上编译Objective-C程序,但是在平台下的编译工具就寥寥无几了,本身这种语言用的人就不是很多。今天在网上突然看到了有人发帖,可以在Windows平台下编译Objective-C,就抱着好奇的心态试了试。没想到,居然成功了,现在就把怎样搭建Objective-C编译平台的经验拿出来和大家分享。
1、安装GNUstep
GNUstep Windows Installer提供了Windows平台下的Object-C的模拟开发环境,一共有四个软件包,其中GNUstep System和GNUstep Core是必装的,GNUstep Devel和Cairo Backend是选装的。只安装前两个就够了。
2、编写Objective-C代码
安装完成后,在开始菜单里的GNUstep选项里执行shell,就能打开命令行。直接在Windows里进入C:/GNUstep/home/Administrator(我的是Administrator,可能有的不一样)目录,在这里用你喜欢的工具(现在UltraEdit和Notepad++编辑器好像可以代码高亮)编写Object-C程序。
如:HelloWorld.m#importint main (int argc, const char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSLog(@"Hello World!"); [pool drain]; return 0; }
3、配置环境变量
这一步很重要。GNUstep.sh是用来设置GNUstep开发环境变量的,如果没有执行,就会有很多头文件,库文件,命令找不到
在一个目录里写好了源代码以后,编写一个make配置文件,名字必须叫GNUmakefile,内容是
include $(GNUSTEP_MAKEFILES)/common.make TOOL_NAME=Test Test_OBJC_FILES=HelloWorld.m include $(GNUSTEP_MAKEFILES)/tool.make
可以修改上面的黑体部分
然后就是
make
命令运行成功就可以看到新增了一个obj目录,里面就有你要的可执行文件和.o文件。
OK 搞定了。
小结:如何在Windows上编译Objective-C的内容介绍完了,希望本文对你有所帮助!
1.下载GNUStep
下载
将下载的GNUStep安装,比如C:\GNUStep
2. 下载JEdit
JEdit 是Freeware,可以用来编辑 .m 文件 .m 是Object C缺省后缀。 .m 相当于 .c 文件
3. 一个Object C教材
———————————————
4. 安装后,执行msys.bat 启动 GNUStep 环境 (类Linux环境)
5. 编写示例程序
fraction.h
#import@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.makeTOOL_NAME = Hello Hello_OBJC_FILES = main.m fraction.minclude $(GNUSTEP_MAKEFILES)/tool.make
6. 编译程序
$ make
将创建 obj目录 运行 hello.exe
The fraction is: 1/3
这样环境就搭好了,你就可以继续学习 Object C了
最终写iphone程序一般还是要Mac OS.
--------------------------------------------------------------------