Windows下安装Object C开发环境,及Hello Word(转)

来源:http://www.cnblogs.com/yaoliang11/archive/2011/02/19/1958430.html

最近想学习iphone开发,但是由于没有c基础,只有java基础。所以先从基础学习,首先是搭建环境,目前手头没有mac机子,只能先在windows下学习基本语法。还好找到了GNUset,可以利用GNUstep在windows下模拟object c开发环境。

官方网址:http://www.gnustep.org/

安装:

GNUstep Windows Installer提供了Windows平台下的Objective-C的模拟开发环境,一共有四个软件包,其中GNUstep SystemGNUstep Core是必装的,GNUstep DevelCairo Backend是选装的。甭管必装选装,一次性全安上,免得以后麻烦。

编写HelloWord

几乎所有的开发环境都是以HelloWord开始,在这里我们先编写HelloWord.

安装完成后,在开始菜单里的GNUstep选项里执行shell,就能打开命令行,在这里就可以使用vi编写Object-C程序了,不过操作起来总有些繁琐,其实也可以直接在Windows里进入C:\GNUstep\home\username目录,在这里用你喜欢的工具编写Object-C程序,然后再进入shell里编译。 
直接给出helloworld.m文件内容,取自Programming in Objective-C 2.0一书:

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

第一次编译:

gcc -o helloworld helloworld.m

结果出现错误信息,找不到头文件:

helloworld.m:1:34: Foundation/Foundation.h: No such file or directory 
helloworld.m: In function `main’: 
helloworld.m:4: error: `NSAutoreleasePool’ undeclared (first use in this function) 
helloworld.m:4: error: (Each undeclared identifier is reported only once 
helloworld.m:4: error: for each function it appears in.) 
helloworld.m:4: error: `pool’ undeclared (first use in this function) 
helloworld.m:5: error: cannot find interface declaration for `NXConstantString’

image 
第二次编译:

gcc -o helloworld helloworld.m \ 
-I /GNUstep/System/Library/Headers/

结果出现错误信息,找不到接口声明:

helloworld.m: In function `main’: 
helloworld.m:5: error: cannot find interface declaration for `NXConstantString’

第三次编译:

 

gcc -o helloworld helloworld.m \ 
-fconstant-string-class=NSConstantString \ 
-I /GNUstep/System/Library/Headers/

 

结果出现错误信息,找不到链接库:

helloworld.m:(.text+0×33): undefined reference to `_objc_get_class’ 
helloworld.m:(.text+0×45): undefined reference to `_objc_msg_lookup’ 
helloworld.m:(.text+0×64): undefined reference to `_objc_msg_lookup’ 
helloworld.m:(.text+0×80): undefined reference to `_NSLog’ 
helloworld.m:(.text+0×93): undefined reference to `_objc_msg_lookup’ 
helloworld.m:(.text+0xbc): undefined reference to `___objc_exec_class’ 
helloworld.m:(.data+0×74): undefined reference to `___objc_class_name_NSAutoreleasePool’ 
helloworld.m:(.data+0×78): undefined reference to `___objc_class_name_NSConstantString’ 
collect2: ld returned 1 exit status

第四次编译:

 

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

 

注意:helloworld.m必须出现在-lobjc和-lgnustep-base的前面,否则会出错。 
此时会出现一些info提示信息,不过不碍事,终于成功了生成了可执行文件,执行看结果。

./helloworld.exe

结果是:

image

注意,可以利用粘贴复制命令:Ctrl+p

—————————————————————————————————————————————————————————————————————————

因为每次输入的内容过多,比较繁琐,因此可以创建gcc.sh文件(文件名称可更改)进行简单命令行输入:

1.在同一文件夹创建gcc.sh文件

2.编辑代码如下:

gcc -o "$1" "$2" -fconstant-string-class=NSConstantString -I/GNUstep/System/Library/Headers -L/GNUstep/System/Library/Libraries -lobjc -lgnustep-base

3.保存

切回命令行之后,输入ls,则可以看到gcc.sh文件。输入命令

sh gcc.sh helloWorld helloWorld.m

则可以看到新生成的helloWorld.exe文件。运行./helloWorld.exe,便可以执行操作。

转载于:https://www.cnblogs.com/yy-is-ing/archive/2012/07/16/2593407.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值