在Windows下学习Objective-c之 开发环境的搭建

系统 1916 0

大家都知道,IOS的软件开发只能在Mac OS操作系统上才能完成. 要使用Xcode作为开发工具. 而苹果的一套电脑动不动就要上万元. 相信很多朋友都想学习IOS软件的开发,但都像我一样很屌丝,买不起苹果的一套装备.不过这都没什么.在前期的Objective-c基础语法学习的时候,基本上用不到Xcode这个工具,在Windows操作系统就可以学习.下面我来讲一下如何在Windows下搭建编译Objective-c程序.

 

第一步:安装GNUstep

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

第二步:编写你自己的 Hello, World!程序

我们怎么知道编译环境是否已经搭建好,就来自己动手写一个helloworld程序编译一下试试嘛.

安装完成后,在开始菜单里的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’

第二次编译:

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看结果,控制台打印出Hello World!

如何使编译更加方便?

现在编译成功后,我们想一下每次编译都输入那么一大串东东,是不是很烦.

下面来告诉大家怎么将编译过程简化.

编辑C:\GNUstep\bin\gcc.sh的文件,内容如下:

#!/bin/sh

if [ $# -ne 1 ]; then
echo "Usage: $0 name"
exit 1
fi

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

exit 0

其中,gcc加入了-g参数,方便gdb调试,使用时就很方便了,注意别带扩展名m:

gcc.sh helloworld 结果如下


在Windows下学习Objective-c之 开发环境的搭建
 

./helloworld.exe


在Windows下学习Objective-c之 开发环境的搭建
 

 

 

 

 

在Windows下学习Objective-c之 开发环境的搭建


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论