日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

在Windows下學習Objective-c之 開發環境的搭建

系統 2847 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條評論
主站蜘蛛池模板: 辉县市| 萨嘎县| 榆中县| 报价| 开平市| 梁平县| 新民市| 瑞安市| 长宁县| 城固县| 黄浦区| 石家庄市| 镇沅| 富民县| 友谊县| 新和县| 上高县| 嘉峪关市| 嘉鱼县| 汉沽区| 罗甸县| 贵州省| 镇远县| 长武县| 循化| 五常市| 乐昌市| 保靖县| 广德县| 龙山县| 漳浦县| 昌黎县| 铁力市| 开鲁县| 平顺县| 桂阳县| 麻城市| 瑞安市| 乐山市| 巩义市| 连山|