原文地址:
此貼轉(zhuǎn)自: http://elton.javaeye.com/blog/344899,不過他好像也是轉(zhuǎn)自別的網(wǎng)站
俗話說,“工欲善其事,必先利其器”,所以學(xué)習(xí)Objective-C的第一件事就是配置Objective-C下面的開發(fā)環(huán)境。有蘋果機(jī)的幸福在于,可以很方便的在XCode下面寫Objective-C的程序。可惜的是不能整天帶著我的大熊貓到處跑,所以也有必要在windows系統(tǒng)下面配置一個(gè)環(huán)境方便學(xué)習(xí)。我們都是被IDE慣壞的孩子,本以為可以很方便做的事情(我是說配置環(huán)境),沒想到居然花了兩天時(shí)間來琢磨怎么搞這個(gè)事情那個(gè),順便又復(fù)習(xí)了一C語言的編譯過程。
安裝
在windows下面想要安裝一個(gè)GNUstep的環(huán)境其實(shí)是很簡單的一件事情。不過說實(shí)話,GNUstep.org上面的文檔還真的是很亂。我為此還安裝了Cygwin和MinGW。事實(shí)上這些都不用安裝,只需要在這里 找到windows installer 就可以了。下載下來的文件有兩個(gè),一個(gè)是GNUstep System,其實(shí)就是MinGW和MSYS,一個(gè)是GNUstep Core,這才是我們需要GNUstep相關(guān)的東西。安裝很簡單,就是windows下面的標(biāo)準(zhǔn)安裝程序。裝完后,在開始菜單里面,有一個(gè)GNUstep 的菜單,點(diǎn)擊shell就可以進(jìn)入MSYS交互環(huán)境了。
第一個(gè)程序
先讓我們來點(diǎn)有成就感的事情。新建一個(gè)文件main.m
> vim main.m
(在你的GNUstep安裝目錄下面的home\<username>文件夾里面,比如我的是C:\GNUstep\home\stelee\ 就會生成一個(gè)main.m文件)
添加如下內(nèi)容
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;
}
然后運(yùn)行g(shù)cc main.m
你就會發(fā)現(xiàn)在同一個(gè)目錄下面有一個(gè)a.exe
在shell環(huán)境下執(zhí)行 ./a.exe就可以看到正確的輸出了。是不是很簡單?基本上來說,這個(gè)第一個(gè)文件雖然是以m結(jié)尾的,但是確是一個(gè)標(biāo)準(zhǔn)的C語言程序,所以我們可以沒有任何障礙的編譯執(zhí)行。那么一個(gè)“真正”意義上的objective-c程序呢?
給你一點(diǎn)挫折
我們修改一下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;
}
這個(gè)程序我們使用了Objective-C的Foundation庫。再執(zhí)行
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慢慢解決
好像是庫文件找不到。這個(gè)沒問題,我們添加一個(gè)庫文件,同時(shí)我們分開執(zhí)行編譯和鏈接,看看都發(fā)生了什么事情
gcc -c main.m -I /GNUstep/System/Library/Headers
main.m:11: error: cannot find interface declaration for `NXConstantString’
在代碼中,我們使用了@”headfile dir is ok\n”,看來需要為編譯器制定默認(rè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
好像編譯成功了,我們的目錄下面有一個(gè)main.o文件。
下面鏈接這個(gè)文件
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
光有頭文件,沒有執(zhí)行的鏈接庫怎么行呢,于是我們得到了最終的命令:
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
趕緊執(zhí)行一下./main.exe看看效果吧
在后續(xù)的文章中我將介紹怎么寫makefile和怎么使用ruby來構(gòu)建自動化編譯過程。不過現(xiàn)在我可以快速的開始我的objective-c的學(xué)習(xí)了!
本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/wuzhe213/archive/2009/10/28/4739978.aspx
posted on 2010-04-12 16:21
漂漂 閱讀(1194)
評論(0) 編輯 收藏 引用