1: GNUstep
首先,目前windows下沒有Objective-C的IDE存在,ObjectiveEClipse是一款可選擇的插件,搭配Eclipse3.5+CDT6.0,但是已經停止更新。GNUstep是提供類似Cocoa(蘋果OS的開發架構)的API和工具,目前支援GNU/Linux and GNU/HURD, Solaris, NetBSD, OpenBSD, FreeBSD, Darwin和Windows,免費使
用。這個項目使Objective C能在多數流行平台上開發和運作。
在Windows下搭建Objective C開發環境,需要到GNUstep官方網站上下載下傳,四個軟體包:GNUstep MSYS System、GNUstep Core、GNUstep Devel、Cairo Backend。其中,前兩個軟體包是必須要安裝的,第三個軟體包是安裝一些開發工具,比如:gcc、g++等,是以如果是學習Objective C的話,這個包也是必須要安裝,第四個軟體包是安裝glib等庫,這個包安裝不安裝根據具體情況而定。
位址:http://www.gnustep.org/experience/Windows.html
安裝路徑不建議出現中文,安裝後在環境變量PATH中增加:
C:\GNUstep\GNUstep\System\Tools;C:\GNUstep\bin;C:\GNUstep\mingw\bin
安裝後運作GNUstep shell也就是安裝目錄下的msys.bat。測試一下gcc與make指令。
2: 測試程式
test.m
1 #import <Foundation/Foundation.h>
2 int main (int argc, const char *argv[]) {
3 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
4 NSLog(@"Hello World!");
5 [pool drain];
6 return 0;
7 }
複制代碼
3: 編譯連結
1) 直接gcc編譯連結方式
gcc -o test test.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base -fconstant-string-class=NSConstantString
其中:
-I /GNUstep/System/Library/Headers 指明編譯期間頭檔案包含目錄
-L /GNUstep/System/Library/Libraries 指明連接配接的庫檔案
-lobjc連結屬性,這樣就不必顯示的連結libobjc.a庫,gcc收到這個連結屬性會為我們完成這些事。
-fconstant-string-class=NSConstantString指定常量字元串類型為NSConstantString
2) GNUmakefile方式
寫GNUmakefile如下:
GNUSTEP_MAKEFILES=/GNUstep/System/Library/Makefiles
include $(GNUSTEP_MAKEFILES)/common.make
TOOL_NAME = test
test_OBJC_FILES = ./main.m
include $(GNUSTEP_MAKEFILES)/tool.make
解釋:其中TOOL_NAME定義為工程名稱test,test_OBJC_FILES定義編譯檔案清單
在GNUmakefile目錄下執行make指令,得到可執行檔案。
3) 搭配IDE,選用CodeBlocks
編譯器設定
使用GNUStep安裝的gcc,在C:\GNUstep\bin目錄下。
(1) Settings->Compiler and debugger...
(2) 選擇GNU GCC Compiler點選copy,重新命名,例如"GNU GCC Obj-C Compiler"
(3) 設定GNU GCC Compiler的Toolchain executables路徑為C:\GNUstep\bin,也就是GNUstep的gcc所在目錄。
(4) Compile settings->Other options添加-fconstant-string-class=NSConstantString
(5) Linker Settings->Other Link Options中添加-lobjc -lgnustep-base選項。
如果出現問題,則可以選用另一種方式,去掉-lobjc -lgnustep-base選項,在Linker Settings->Link libraries中添加:
C:/GNUstep/GNUstep/System/Library/Libraries/libobjc.dll.a
C:/GNUstep/GNUstep/System/Library/Libraries/libgnustep-base.dll.a
(6) Search directories->Complier添加頭檔案目錄: C:\GNUstep\GNUstep\System\Library\Headers
添加源檔案格式支援
1) Environment...,選擇Files extension handling添加 *.m和*.mm
2) Project->Project tree, file types & categories...在Source中添加*.m和*.mm
高亮顯示
1) Settings->Editor->Syntax highlighting
2) 選擇Filemasks...,添加*.m和*.mm
3) 選擇 Keywords... 添加Keywords到清單框中
Keywords:
@interface @implementation @end @class @selector @protocol @public @protected @private id BOOL YES NO SEL nil NULL self
設定為可編譯連結
1) .m檔案右鍵->Properties
2) 選擇build,選中 Compile file 和 Link file
3) 選擇general,去除對File is read-only的選中
4) 注意,.h檔案不要設定Compile file 和 Link file