2012-02-18 59 views
10

我正在學習Objective-C語言。由於我沒有Mac,我正在Ubuntu 11.04平臺中編譯和運行我的代碼。用Clang編譯Objective C時遇到的問題(Ubuntu)

到現在爲止,我使用gcc編譯。我已經安裝了GNUStep,並且一切正常。但後來我開始嘗試一些Objective-C 2.0功能,例如@property和@synthesize,gcc不允許。

所以我試着用Clang編譯代碼,但它似乎沒有將我的代碼與GNUStep庫正確鏈接,甚至沒有使用簡單的Hello World程序。

例如,如果我編譯下面的代碼:

#import <Foundation/Foundation.h> 

int main(void) { 
    NSLog(@"Hello world!"); 
    return 0; 
} 

編譯器的輸出是:

/tmp/cc-dHZIp1.o: In function `main': 
test.m:(.text+0x1f): undefined reference to `NSLog' 
/tmp/cc-dHZIp1.o: In function `.objc_load_function': 
test.m:(.text+0x3c): undefined reference to `__objc_exec_class' 
collect2: ld returned 1 exit status 
clang: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation) 

我使用編譯的命令是

clang -I /usr/include/GNUstep/ test.m -o test 
帶有-I指令的

包含GNUStep庫(否則,Clang無法找到Foundation.h)。

我已經GOOGLE了我的問題,並訪問了GNUStep和Clang網頁,但我還沒有找到解決方案。所以任何幫助將不勝感激。

謝謝!

回答

1

你可以試試gcc編譯:
首先安裝GNU Objective-C運行的:sudo apt-get install gobjc
然後編譯:gcc -o hello hello.m -Wall -lobjc

+0

嗨URLArenzo。我已經安裝了該軟件包,並嘗試gcc編譯(它工作得很完美)。但是,我發現gcc不支持Objective-C 2.0的某些功能;所以我試圖用Clang編譯器運行。 – 2012-02-18 16:22:41

6

的問題是,沒有被使用的鏈接庫GNUstep的基地。因此,要解決這個用的是選項-Xlinker,發送參數由鐺用的鏈接:

clang -I /usr/include/GNUstep/ -Xlinker -lgnustep-base test.m -o test 

聲明「-X連接-lgnustep基地」製造的魔力。然而,我與這個命令相關的類,它表示在Objective-C的字符串問題:

./test: Uncaught exception NSInvalidArgumentException, reason: GSFFIInvocation: 
Class 'NXConstantString'(instance) does not respond to forwardInvocation: for 
'hasSuffix:' 

我能解決它添加參數「-fconstant串級= NSConstantString」:

clang -I /usr/include/GNUstep/ -fconstant-string-class=NSConstantString \ 
-Xlinker -lgnustep-base test.m -o test 

此外,我已經嘗試了一些Objective-C 2.0代碼,它似乎工作。

謝謝你的幫助!