文章目錄
- Windows下安裝Object C開發環境,及Hello Word
Windows下安裝Object C開發環境,及Hello Word
最近想學習iphone開發,但是由於沒有c基礎,只有java基礎。所以先從基礎學習,首先是搭建環境,目前手頭沒有mac機子,只能先在windows下學習基本文法。還好找到了GNUset,可以利用GNUstep在windows下類比object c開發環境。
官方網址:http://www.gnustep.org/
安裝:
GNUstep Windows Installer提供了Windows平台下的Objective-C的類比開發環境,一共有四個軟體包,其中GNUstep System和GNUstep Core是必裝的,GNUstep Devel和Cairo 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’
第二次編譯:
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
結果是:
注意,可以利用粘貼複製命令:Ctrl+p