﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-flyonok-随笔分类-iphone</title><link>http://www.cppblog.com/flyonok/category/13426.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 28 Apr 2010 10:12:35 GMT</lastBuildDate><pubDate>Wed, 28 Apr 2010 10:12:35 GMT</pubDate><ttl>60</ttl><item><title>objective c develop environment setup</title><link>http://www.cppblog.com/flyonok/archive/2010/03/31/111180.html</link><dc:creator>flyonok</dc:creator><author>flyonok</author><pubDate>Wed, 31 Mar 2010 08:23:00 GMT</pubDate><guid>http://www.cppblog.com/flyonok/archive/2010/03/31/111180.html</guid><wfw:comment>http://www.cppblog.com/flyonok/comments/111180.html</wfw:comment><comments>http://www.cppblog.com/flyonok/archive/2010/03/31/111180.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyonok/comments/commentRss/111180.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyonok/services/trackbacks/111180.html</trackback:ping><description><![CDATA[Mac笔记本实在是<a target="_blank" href="http://www.machome.com.cn/thread-33574-1-1.html">贵</a>，所以一直没舍得买，如此
一来，就只能在我的Windows操作系统上学Objective-C了。<br>
<br>
<strong> 安装GNUstep</strong> <br>
<br>
GNUstep Windows Installer提供了Windows平台下的Objective-C的模拟开发环境，一共有四个软件包，其中<a href="http://ftpmain.gnustep.org/pub/gnustep/binaries/windows/gnustep-system-0.23.0-setup.exe" target="_blank">GNUstep System</a>和<a href="http://ftpmain.gnustep.org/pub/gnustep/binaries/windows/gnustep-core-0.23.0-setup.exe" target="_blank">GNUstep Core</a>是必装的，<a href="http://ftpmain.gnustep.org/pub/gnustep/binaries/windows/gnustep-devel-1.0.0-setup.exe" target="_blank">GNUstep Devel</a>和<a href="http://ftpmain.gnustep.org/pub/gnustep/binaries/windows/gnustep-cairo-0.22.1-setup.exe" target="_blank">Cairo Backend</a>是选装的。甭管必装选装，一次性全安上，免得以后麻烦。<br>
<br>
<strong> 编写Hello, World!</strong> <br>
<br>
安装完成后，在开始菜单里的GNUstep选项里执行shell，就能打开命令行，在这里就可以使用vi编写Object-C程序了，不过操作起来总有些
繁琐，其实也可以直接在Windows里进入C:\GNUstep\home\<em> username</em> 目录，在这里用你喜欢的工具编写
Object-C程序，然后再进入shell里编译。<br>
<br>
直接给出helloworld.m文件内容，取自Programming in Objective-C 2.0一书：<br>
<br>
<font face="宋体">#import &lt;Foundation/Foundation.h&gt;<br>
<br>
int main (int argc, const char *argv[]) {<br>
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];<br>
NSLog(@"Hello World!");<br>
[pool drain];<br>
<br>
return 0;<br>
}</font><br>
<br>
<strong> 第一次编译：</strong> <br>
<br>
gcc -o helloworld helloworld.m<br>
<br>
结果出现错误信息，找不到头文件：<br>
<br>
helloworld.m:1:34: Foundation/Foundation.h: No such file or directory<br>
helloworld.m: In function `main':<br>
helloworld.m:4: error: `NSAutoreleasePool' undeclared (first use in this
function)<br>
helloworld.m:4: error: (Each undeclared identifier is reported only once<br>
helloworld.m:4: error: for each function it appears in.)<br>
helloworld.m:4: error: `pool' undeclared (first use in this function)<br>
helloworld.m:5: error: cannot find interface declaration for
`NXConstantString'<br>
<br>
<strong> 第二次编译：</strong> <br>
<br>
gcc -o helloworld helloworld.m \<br>
-I /GNUstep/System/Library/Headers/<br>
<br>
结果出现错误信息，找不到接口声明：<br>
<br>
helloworld.m: In function `main':<br>
helloworld.m:5: error: cannot find interface declaration for
`NXConstantString'<br>
<br>
<strong> 第三次编译：</strong> <br>
<br>
gcc -o helloworld helloworld.m \<br>
-fconstant-string-class=NSConstantString \<br>
-I /GNUstep/System/Library/Headers/<br>
<br>
结果出现错误信息，找不到链接库：<br>
<br>
helloworld.m:(.text+0x33): undefined reference to `_objc_get_class'<br>
helloworld.m:(.text+0x45): undefined reference to `_objc_msg_lookup'<br>
helloworld.m:(.text+0x64): undefined reference to `_objc_msg_lookup'<br>
helloworld.m:(.text+0x80): undefined reference to `_NSLog'<br>
helloworld.m:(.text+0x93): undefined reference to `_objc_msg_lookup'<br>
helloworld.m:(.text+0xbc): undefined reference to `___objc_exec_class'<br>
helloworld.m:(.data+0x74): undefined reference to
`___objc_class_name_NSAutoreleasePool'<br>
helloworld.m:(.data+0x78): undefined reference to
`___objc_class_name_NSConstantString'<br>
collect2: ld returned 1 exit status<br>
<br>
<strong> 第四次编译：</strong> <br>
<br>
gcc -o helloworld helloworld.m \<br>
-fconstant-string-class=NSConstantString \<br>
-I /GNUstep/System/Library/Headers/ \<br>
-L /GNUstep/System/Library/Libraries/ \<br>
-lobjc \<br>
-lgnustep-base<br>
<br>
注意：helloworld.m必须出现在-lobjc和-lgnustep-base的前面，否则会出错。<br>
<br>
此时会出现一些info提示信息，不过不碍事，终于成功了生成了可执行文件，执行./helloworld.exe看结果。<br>
<br>
<strong> 快捷方式：</strong> <br>
<br>
如果每次使用gcc的时候，都要输入这么长的命令，无疑是很恼火的事儿，我们可以做一个快捷方式：<br>
<br>
编辑C:\GNUstep\bin\gcc.sh的文件，内容如下：<br>
<br>
#!/bin/sh<br>
<br>
if [ $# -ne 1 ]; then<br>
echo "Usage: $0 name"<br>
exit 1<br>
fi<br>
<br>
gcc -g -o $1 $1.m \<br>
-fconstant-string-class=NSConstantString \<br>
-I /GNUstep/System/Library/Headers/ \<br>
-L /GNUstep/System/Library/Libraries/ \<br>
-lobjc \<br>
-lgnustep-base<br>
<br>
exit 0<br>
<br>
其中，gcc加入了-g参数，方便gdb调试，使用时就很方便了，注意别带扩展名m：<br>
<br>
gcc.sh helloworld<br>
<br>
参考链接：<a href="http://blog.lyxite.com/2008/01/compile-objective-c-programs-using-gcc.html" target="_blank">Compile Objective-C Programs Using gcc</a>
<br><img src ="http://www.cppblog.com/flyonok/aggbug/111180.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyonok/" target="_blank">flyonok</a> 2010-03-31 16:23 <a href="http://www.cppblog.com/flyonok/archive/2010/03/31/111180.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>