安裝GNUstephtml
GNUstep Windows Installer提供了Windows平臺下的Objective-C的模擬開發環境,一共有四個軟件包,其中GNUstep System和GNUstep Core是必裝的,GNUstep Devel和Cairo Backend是選裝的。甭管必裝選裝,一次性全安上,省得之後麻煩。objective-c
編寫Hello, World!shell
安裝完成後,在開始菜單裏的GNUstep選項裏執行shell,就能打開命令行,在這裏就能夠使用vi編寫Object-C程序了,不過操做起來總有些繁瑣,其實也能夠直接在Windows裏進入C:\GNUstep\home\username目錄,在這裏用你喜歡的工具編寫Object-C程序,而後再進入shell裏編譯。windows
直接給出helloworld.m文件內容,取自Programming in Objective-C 2.0一書:app
#import <Foundation/Foundation.h>iphone
int main (int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Hello World!");
[pool drain];ide
return 0;
}工具
第一次編譯:this
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看結果。
快捷方式:
若是每次使用gcc的時候,都要輸入這麼長的命令,無疑是很惱火的事兒,咱們能夠作一個快捷方式:
編輯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
參考連接:
Compile Objective-C Programs Using gcc
http://blog.joomla.org.tw/mobile/59-iphone/103-objective-c-gnustep.html