[objective-c] win下搭建Objective-c开发环境

GNUstep


  首先,目前windows下没有Objective-C的IDE存在,ObjectiveEClipse是一款可选择的插件,搭配Eclipse3.5+CDT6.0,但是已经停止更新。GNUstep是提供类似Cocoa(苹果OS的开发框架)的API和工具,目前支持GNU/Linux and GNU/HURD, Solaris, NetBSD, OpenBSD, FreeBSD, Darwin和Windows,免费使用。这个项目使Objective C能在多数流行平台上开发和运行。

  在Windows下搭建Objective C开发环境,需要到GNUstep官方网站上下载,四个软件包:GNUstep MSYS SystemGNUstep CoreGNUstep DevelCairo Backend。其中,前两个软件包是必须要安装的,第三个软件包是安装一些开发工具,比如:gcc、g++等,所以如果是学习Objective C的话,这个包也是必须要安装,第四个软件包是安装glib等库,这个包安装不安装根据具体情况而定。安装路径不建议出现中文,安装后在环境变量PATH中增加: C:\GNUstep\GNUstep\System\Tools;C:\GNUstep\bin;C:\GNUstep\mingw\bin,安装后运行GNUstep shell也就是安装目录下的msys.bat。测试一下gcc与make命令。

测试程序 

1  #import <Foundation/Foundation.h>
2 int main (int argc, const char *argv[]) {
3 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
4 NSLog(@"Hello World!");
5 [pool drain];
6 return 0;
7 }

编译链接


 1:直接gcc编译链接方式

  gcc -o test test.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base -fconstant-string-class=NSConstantString

  其中:

    -I /GNUstep/System/Library/Headers  指明编译期间头文件包含目录

    -L /GNUstep/System/Library/Libraries 指明连接的库文件

    -lobjc链接属性,这样就不必显示的链接libobjc.a库,gcc收到这个链接属性会为我们完成这些事。

    -fconstant-string-class=NSConstantString指定常量字符串类型为NSConstantString

2:GNUmakefile方式

  写GNUmakefile如下:

  GNUSTEP_MAKEFILES=/GNUstep/System/Library/Makefiles
  include $(GNUSTEP_MAKEFILES)/common.make
  TOOL_NAME = test
  test_OBJC_FILES = ./main.m
  include $(GNUSTEP_MAKEFILES)/tool.make

 注:其中TOOL_NAME定义为工程名称test,test_OBJC_FILES定义编译文件列表

   在GNUmakefile目录下执行make命令,得到可执行文件。

3:搭配IDE,选用CodeBlocks

编译器设定


 使用GNUStep安装的gcc,在C:\GNUstep\bin目录下。

1)   Settings->Compiler and debugger...

2)   选择GNU GCC Compiler点击copy,重新命名,例如"GNU GCC Obj-C Compiler"

3)   设定GNU GCC CompilerToolchain executables路径为C:\GNUstep\bin,也就是GNUstep的gcc所在目录。

4)   Compile settings->Other options添加-fconstant-string-class=NSConstantString

5)    Linker Settings->Other Link Options中添加-lobjc -lgnustep-base选项。

  如果出现问题,则可以选用另一种方式,去掉-lobjc -lgnustep-base选项,在Linker Settings->Link libraries中添加:

  C:/GNUstep/GNUstep/System/Library/Libraries/libobjc.dll.a  

  C:/GNUstep/GNUstep/System/Library/Libraries/libgnustep-base.dll.a

6)   Search directories->Complier添加头文件目录: C:\GNUstep\GNUstep\System\Library\Headers

 

添加源文件格式支持


1) Environment...选择Files extension handling添加 *.m*.mm
2)Project->Project tree, file types & categories...Source中添加*.m和*.mm
 

高亮显示


1) Settings->Editor->Syntax highlighting

2) 选择Filemasks...,添加*.m和*.mm

3) 选择 Keywords... 添加Keywords到列表框中

Keywords:

@interface @implementation @end @class @selector @protocol @public @protected @private id BOOL YES NO SEL nil NULL self


设置为可编译链接


1) .m文件右键->Properties

2) 选择build,选中 Compile file 和 Link file

3) 选择general,去除对File is read-only的选中

4) 注意,.h文件不要设置Compile file 和 Link file

posted @ 2011-10-27 21:18  zsounder  阅读(4554)  评论(1编辑  收藏  举报