大家都知道,IOS的軟件開發(fā)只能在Mac OS操作系統(tǒng)上才能完成. 要使用Xcode作為開發(fā)工具. 而蘋果的一套電腦動(dòng)不動(dòng)就要上萬元. 相信很多朋友都想學(xué)習(xí)IOS軟件的開發(fā),但都像我一樣很屌絲,買不起蘋果的一套裝備.不過這都沒什么.在前期的Objective-c基礎(chǔ)語(yǔ)法學(xué)習(xí)的時(shí)候,基本上用不到Xcode這個(gè)工具,在Windows操作系統(tǒng)就可以學(xué)習(xí).下面我來講一下如何在Windows下搭建編譯Objective-c程序.
?
第一步:安裝GNUstep
GNUstep Windows Installer提供了Windows平臺(tái)下的Objective-C的模擬開發(fā)環(huán)境,一共有四個(gè)軟件包,其中 GNUstep System 和 GNUstep Core 是必裝的, GNUstep Devel 和 Cairo Backend 是選裝的。甭管必裝選裝,一次性全安上,免得以后麻煩。
第二步:編寫你自己的 Hello, World!程序
我們?cè)趺粗谰幾g環(huán)境是否已經(jīng)搭建好,就來自己動(dòng)手寫一個(gè)helloworld程序編譯一下試試嘛.
安裝完成后,在開始菜單里的GNUstep選項(xiàng)里執(zhí)行shell,就能打開命令行,在這里就可以使用vi編寫Object-C程序了,不過操作起來總有些繁瑣,其實(shí)也可以直接在Windows里進(jìn)入C:\GNUstep\home\ username 目錄,在這里用你喜歡的工具編寫Object-C程序,然后再進(jìn)入shell里編譯。
直接給出helloworld.m文件內(nèi)容,取自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
結(jié)果出現(xiàn)錯(cuò)誤信息,找不到頭文件:
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/
結(jié)果出現(xiàn)錯(cuò)誤信息,找不到接口聲明:
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/
結(jié)果出現(xiàn)錯(cuò)誤信息,找不到鏈接庫(kù):
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必須出現(xiàn)在-lobjc和-lgnustep-base的前面,否則會(huì)出錯(cuò)。
此時(shí)會(huì)出現(xiàn)一些info提示信息,不過不礙事.
終于成功了生成了可執(zhí)行文件,執(zhí)行./helloworld.exe看結(jié)果,控制臺(tái)打印出Hello World!
如何使編譯更加方便?
現(xiàn)在編譯成功后,我們想一下每次編譯都輸入那么一大串東東,是不是很煩.
下面來告訴大家怎么將編譯過程簡(jiǎn)化.
編輯C:\GNUstep\bin\gcc.sh的文件,內(nèi)容如下:
#!/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參數(shù),方便gdb調(diào)試,使用時(shí)就很方便了,注意別帶擴(kuò)展名m:
gcc.sh helloworld 結(jié)果如下
./helloworld.exe
?
?
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
