2012-03-15 64 views
2

在嘗試使用clang編譯時,出現以下Objective-C代碼的錯誤。無法用叮噹編譯Objective-C代碼

的OBJ-C代碼:

// first program example 

#import <Foundation/Foundation.h> 

int main (int argc, const char * argv[]) { 
    @autoreleasepool { 
     NSLog (@"Programming is fun!"); 
    } 
    return 0; 
} 

錯誤消息:

main.m:6:5: error: unexpected '@' in program 
    @autoreleasepool { 
    ^
main.m:7:38: error: extraneous ')' before ';' 
    NSLog (@"Programming is fun!"); 
           ^
main.m:7:16: warning: expression result unused [-Wunused-value] 
    NSLog (@"Programming is fun!"); 
      ^~~~~~~~~~~~~~~~~~~~~~ 
main.m:9:5: error: expected identifier or '(' 
    return 0; 
    ^
main.m:10:1: error: expected external declaration 
} 
^ 
1 warning and 4 errors generated. 

我的XCode內沒有錯誤編譯。

鏘信息: 蘋果鐺版本2.1(標籤/蘋果/鐺-163.7.1)(基於LLVM 3.0svn) 目標:x86_64的 - 蘋果darwin11.3.0 線程模型:POSIX

+3

您正在使用什麼版本鐺的? '@autoreleasepool {}'是新的,而且Clang正在快速移動。 – 2012-03-15 19:21:21

回答

5

您需要使用鐺鐺v3.0或更高版本才能使用@autoreleasepool

+0

好的,很高興知道。我只是想通過MacPorts更新clang,然後告訴我2.9版已經發布,所以我安裝了clang 2.9,它仍在安裝。我想我將不得不刪除該版本,並將在安裝3.x後。 – cherrun 2012-03-15 21:12:07

+0

好的,出於某種原因,我無法在我的系統上安裝最新版本的clang。安裝它的工作,但它仍然會使用舊版本。我今天剛剛安裝了最新的XCode,根據這篇文章的第二個答案,我應該已經在我的系統上安裝了鏗鏘3.0版http://stackoverflow.com/questions/8674546/how-to-update-llvm-clang-on -mac-osx – cherrun 2012-03-16 00:02:47

+0

好吧,我想通了。命令行工具不再包含在最新的XCode版本中(4.3.1)。因此,您需要從這裏手動下載和安裝:https://developer.apple.com/downloads/index.action# – cherrun 2012-03-16 07:40:32

2

你你需要升級你的叮噹; @autoreleasepool{}指令與ARC同時發佈,並且要求v3.0或更高版本。

3

假設你有你的系統路徑鐺3.0,你可以編譯代碼:

clang -Wall -framework Foundation prog_name.m -o prog_name