[Objective-C] win下搭建Objective-c开发环【[转】

1: 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等库,这个包安装不安装根据具体情况而定。

  地址:http://www.gnustep.org/experience/Windows.html

  安装路径不建议出现中文,安装后在环境变量PATH中增加:

  C:\GNUstep\GNUstep\System\Tools;C:\GNUstep\bin;C:\GNUstep\mingw\bin

  安装后运行GNUstep shell也就是安装目录下的msys.bat。测试一下gcc与make命令。

2: 测试程序

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 #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 }

复制代码

3: 编译链接

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

上述开发环境配置完成后,就可以开始代码测试了。

首先,新建一个工程,选择File->New->Project…,会出现一个工程类型窗口,选择Console Application,然后按照工程建立指引,建立一个mytest的工程,并将main.c的文件更名为main.m,录入以下代码:

#import <Foundation/Foundation.h>

int main (int argc, const char *argv[])

{

NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];

NSLog(@"%@",@"hello world");

[pool drain];

return 0;

}

如图:

之后再开始编译运行:Buid –> Run… 如果出现以下窗口,恭喜你,你已经成功的搭建了Windows下的Objective-C的集成开发环境。

设置为可编译链接

1) .m文件右键->Properties

2) 选择build,选中 Compile fileLink file

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

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