1. 安裝編譯環境
Object C和其他很多語言一樣,都需要有一個編譯器。Object C 是在GCC下編譯的。GCC(GNU Compiler Collection,GNU編譯器集合),是一套由 GNU 開發的編程語言編譯器。很多人想到學習Object C就想到mac電腦,想到XCode開發工具。其實在Windows環境一下也可以編譯Object C。
首先下載Windows版本的GCC編譯器,下載地址:http://wwwmain.gnustep.org/resources/downloads.php
下載如下幾個包:
gnustep-system-0.23.0-setup.exe gnustep-core-0.23.0-setup.exe gnustep-devel-1.0.0-setup.exe gnustep-cairo-0.22.1-setup.exe
以上四個包點擊鏈接可以下載,下載之后安裝順序安裝,前面兩個包是必選的,后面兩個是可選安裝的。
2. 安裝IDE開發環境
CodeBlocks IDE是一個開源跨平台的C++ 開發工具。其官網地址:http://www.codeblocks.org/
下載地址如下:http://www.codeblocks.org/downloads/26
工具界面如圖:
3. 配置編譯環境
安裝好工具之后,打開如上圖界面,在導航菜單欄中找到Settings--Compiler Settings
重新命名為"GNUstep MinGW Compiler", 大部分人都是這么命名的。然后Set as default
編譯設置參數:選擇Compiler Settings 選項卡中選擇Other Options選項卡,在其中輸入: "-fconstant-string-class=NSConstantString -std=c99"
設置Linker Settings:在"\GNUstep\GNUstep\System\Library\Libraries\" 安裝目錄下找到 libgnustep-base.dll.a libobjc.dll.a 兩個文件
設置Search directories : 將"\GNUstep\GNUstep\System\Library\Headers" 目錄配置到Compiler選項中
4. 配置語法、文件類型,關鍵字等
(1)進入Settings->Environment...
(2)選擇 Files extension handling 添加*.m
(3)進入 Project->Project tree->Edit file types & categories...
(4)在Sources, 下面添加 *.m到文件類型列表中.
5. 新建工程,測試Object C
新建一個控制台程序,如上圖所示:
#import <Foundation/Foundation.h> int main (int argc, const char *argv[]) { NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init]; NSLog(@"%@",@"第一個測試程序"); [pool drain]; return 0; }
點擊Build - run 或者 Ctrl + F10 ,編譯報錯:
ERROR: You need to specify a debugger program in the debuggers's settings.
(For MinGW compilers, it's 'gdb.exe' (without the quotes))
(For MSVC compilers, it's 'cdb.exe' (without the quotes))
如上問題需要設置一下:Settings--Compiler Settings--Toolchain executables
點擊Auto-detect 之后會自動設置相應的環境配置,然后重新編譯即可. 運行效果如下: