原文地址:
此貼轉自: http://elton.javaeye.com/blog/344899,不過他好像也是轉自別的網站
俗話說,“工欲善其事,必先利其器”,所以學習Objective-C的第一件事就是配置Objective-C下面的開發環境。有蘋果機的幸福在于,可以很方便的在XCode下面寫Objective-C的程序。可惜的是不能整天帶著我的大熊貓到處跑,所以也有必要在windows系統下面配置一個環境方便學習。我們都是被IDE慣壞的孩子,本以為可以很方便做的事情(我是說配置環境),沒想到居然花了兩天時間來琢磨怎么搞這個事情那個,順便又復習了一C語言的編譯過程。
安裝
在windows下面想要安裝一個GNUstep的環境其實是很簡單的一件事情。不過說實話,GNUstep.org上面的文檔還真的是很亂。我為此還安裝了Cygwin和MinGW。事實上這些都不用安裝,只需要在這里 找到windows installer 就可以了。下載下來的文件有兩個,一個是GNUstep System,其實就是MinGW和MSYS,一個是GNUstep Core,這才是我們需要GNUstep相關的東西。安裝很簡單,就是windows下面的標準安裝程序。裝完后,在開始菜單里面,有一個GNUstep 的菜單,點擊shell就可以進入MSYS交互環境了。
第一個程序
先讓我們來點有成就感的事情。新建一個文件main.m
> vim main.m
(在你的GNUstep安裝目錄下面的home\<username>文件夾里面,比如我的是C:\GNUstep\home\stelee\ 就會生成一個main.m文件)
添加如下內容
Objective-c代碼
#import <stdio.h>
int main(int argc,const char *argv[]){
printf(”hello world\n”);
return 0;
}
#import <stdio.h>
int main(int argc,const char *argv[]){
printf(”hello world\n”);
return 0;
}
然后運行gcc main.m
你就會發現在同一個目錄下面有一個a.exe
在shell環境下執行 ./a.exe就可以看到正確的輸出了。是不是很簡單?基本上來說,這個第一個文件雖然是以m結尾的,但是確是一個標準的C語言程序,所以我們可以沒有任何障礙的編譯執行。那么一個“真正”意義上的objective-c程序呢?
給你一點挫折
我們修改一下main.m程序
Objective-c代碼
#import <Foundation/Foundation.h>
int main(int argc, char**argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@”headfile dir is ok\n”);
[pool release];
return 0;
}
#import <Foundation/Foundation.h>
int main(int argc, char**argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@”headfile dir is ok\n”);
[pool release];
return 0;
}
這個程序我們使用了Objective-C的Foundation庫。再執行
gcc main.m
main.m:1:34: Foundation/Foundation.h: No such file or directory
main.m: In function `main’:
main.m:7: error: `NSAutoreleasePool’ undeclared (first use in this function)
main.m:7: error: (Each undeclared identifier is reported only once
main.m:7: error: for each function it appears in.)
main.m:7: error: `pool’ undeclared (first use in this function)
main.m:11: error: cannot find interface declaration for `NXConstantString’
Step by step慢慢解決
好像是庫文件找不到。這個沒問題,我們添加一個庫文件,同時我們分開執行編譯和鏈接,看看都發生了什么事情
gcc -c main.m -I /GNUstep/System/Library/Headers
main.m:11: error: cannot find interface declaration for `NXConstantString’
在代碼中,我們使用了@”headfile dir is ok\n”,看來需要為編譯器制定默認的Constant String類型,別忘了,這是C語言嘛
Shell代碼
gcc -fconstant-string-class=NSConstantString -c main.m -I /GNUstep/System/Library/Headers
gcc -fconstant-string-class=NSConstantString -c main.m -I /GNUstep/System/Library/Headers
好像編譯成功了,我們的目錄下面有一個main.o文件。
下面鏈接這個文件
gcc -o main main.o
main.o:main.m:(.text+0×33): undefined reference to `objc_get_class’
main.o:main.m:(.text+0×45): undefined reference to `objc_msg_lookup’
main.o:main.m:(.text+0×64): undefined reference to `objc_msg_lookup’
main.o:main.m:(.text+0×80): undefined reference to `NSLog’
main.o:main.m:(.text+0×93): undefined reference to `objc_msg_lookup’
main.o:main.m:(.text+0xbc): undefined reference to `__objc_exec_class’
main.o:main.m:(.data+0×74): undefined reference to `__objc_class_name_NSAutorele
asePool’
main.o:main.m:(.data+0×78): undefined reference to `__objc_class_name_NSConstant
String’
collect2: ld returned 1 exit status
光有頭文件,沒有執行的鏈接庫怎么行呢,于是我們得到了最終的命令:
Shell代碼
gcc -o main main.o -L /GNUstep/System/Library/Libraries/ -lobjc -lgnustep-base
gcc -o main main.o -L /GNUstep/System/Library/Libraries/ -lobjc -lgnustep-base
最終得到了我們想要的main.exe
趕緊執行一下./main.exe看看效果吧
在后續的文章中我將介紹怎么寫makefile和怎么使用ruby來構建自動化編譯過程。不過現在我可以快速的開始我的objective-c的學習了!
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/wuzhe213/archive/2009/10/28/4739978.aspx