標籤:
學習Object-c如果使用的是Windows,一般推薦使用虛擬機器,但是太重量級了,先要下載OS-X,又要下載x-code。這裡推薦一種比較簡便的方式,使用code-block來搭建簡易的Object-c學習環境,是:http://www.codblocks.org/。
Objective-C的編譯器有很多,這裡使用GnuStep,網址是http://www.gnustep.org/experience/Windows.html,從這裡可以下載Windows版本的gcc編譯器,共有四個軟體包,其中GNUstep System和GNUstep Core是必裝的,GNUstep Devel和Cairo Backend是選裝的(用迅雷拖要快點)。
下載並安裝好Code-block 和 GnuStep 之後,對Code-block進行配置。
開啟Code::Blocks,點擊菜單Settings>Compiler and debugger>Global compiler settings在Selected compiler下拉框下面點擊Copy, 在快顯視窗中填入: GNUstep MinGW Compiler之後,點擊Toolchain executables選項卡,將Compiler’s installation directory選擇為C:\GNUstep,如:
接下來找到Other Options 選項卡,寫入:-fconstant-string-class=NSConstantString -std=c99 :
接下來設定靜態連結庫,在Link settings 中進行添加,需要添加的.a檔案在:C:\GNUstep\GNUstep\System\Library\Libraries: libgnustep-base.dll.a 和 libobjc.dll.a,
接下來在Search irectories 中的Compiler選項卡中增加:C:\GNUstep\GNUstep\System\Library\Headers;在Linker選項卡中增加:C:\GNUstep\GNUstep\System\Library\Libraries。這裡就不了。
進入Settings->Environment...,選擇Files extension handling 添加*.m;進入 Project->Projecttree->Edit file types & categories... ,在Sources, 下面添加*.m到檔案類型列表中。
進入Settings->Editor...,選擇 Syntaxhighlighting,點擊“Filemasks....”按鈕,在彈出框尾部添加*.m 到檔案類型;點擊“Keywords...”按鈕 (緊靠Filemasks...按鈕) 添加下面Object-C的關鍵字到EditKeywords列表中:@interface @implementation @end @class @selector @protocol @public @protected @private id BOOL YES NO SEL nil NULL self。
最後進行測試,選擇File->New->Project…,會出現一個工程類型視窗,選擇Console Application,然後按照工程建立指引,建立一個mTest的工程,並將main.c的檔案更名為main.m,寫入代碼:
#import <Foundation/Foundation.h>int main (int argc, const char * argv[]){ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSDate *now = [NSDate date]; NSLog(@"This NSDate object lives at %p", now); NSLog(@"The date is %@", now); [pool drain]; return 0;}
編譯運行上述代碼,結果如下:
使用Codeblock搭建Windows下Objec-c學習環境