windows下開發OOPC,hello world—-

來源:互聯網
上載者:User

Mac筆記本實在是貴,所以一直沒捨得買,如此一來,就只能在我的Windows作業系統上學Objective-C了。

安裝GNUstep

GNUstep Windows Installer提供了Windows平台下的Objective-C的類比開發環境,一共有四個軟體包,其中GNUstep System和GNUstep Core是必裝的,GNUstep Devel和Cairo Backend是選裝的。甭管必裝選裝,一次性全安上,免得以後麻煩。

編寫Hello, World!

安裝完成後,在開始菜單裡的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+0x33): undefined reference to `_objc_get_class'
helloworld.m:(.text+0x45): undefined reference to `_objc_msg_lookup'
helloworld.m:(.text+0x64): undefined reference to `_objc_msg_lookup'
helloworld.m:(.text+0x80): undefined reference to `_NSLog'
helloworld.m:(.text+0x93): undefined reference to `_objc_msg_lookup'
helloworld.m:(.text+0xbc): undefined reference to `___objc_exec_class'
helloworld.m:(.data+0x74): undefined reference to `___objc_class_name_NSAutoreleasePool'
helloworld.m:(.data+0x78): 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://hi.baidu.com/thinkinginlamp/blog/category/c%20%26%2347%3B%20objective-c

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.