【问题标题】:Can't compile Objective-C code with clang无法使用 clang 编译 Objective-C 代码
【发布时间】:2012-04-01 08:39:28
【问题描述】:

在尝试使用 clang 编译时,我在使用以下 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 中正确编译。

Clang 信息: Apple clang 2.1 版 (tags/Apple/clang-163.7.1) (基于 LLVM 3.0svn) 目标:x86_64-apple-darwin11.3.0 线程模型:posix

【问题讨论】:

  • 你用的是什么版本的clang? @autoreleasepool{} 是新的,Clang 正在快速发展。

标签: objective-c clang


【解决方案1】:

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

【讨论】:

  • 好的,很高兴知道。我只是想通过 MacPorts 更新 clang,然后它告诉我 2.9 版本已经发布,所以我安装了 clang 2.9,它仍在安装中。我想我必须删除那个版本,然后再安装 3.x。
  • 好的,由于某种原因,我无法在我的系统上安装最新版本的 clang。安装它可以工作,但它仍将使用旧版本。我今天刚刚安装了最新的XCode,根据这篇文章的第二个答案,我的系统上应该已经安装了clang 3.0 stackoverflow.com/questions/8674546/…
  • 好的,我想通了。命令行工具不再包含在最新的 XCode 版本 (4.3.1) 中。因此您需要从这里手动下载和安装:developer.apple.com/downloads/index.action#
  • @cherrun:您也可以从 Xcode 首选项窗口安装它们。
【解决方案2】:

假设你的系统路径上有 clang 3.0,你可以编译你的代码:

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

【讨论】:

    【解决方案3】:

    您需要升级您的 Clang; @autoreleasepool{} 指令与 ARC 同时发布,需要 v3.0 或更高版本。

    【讨论】:

      猜你喜欢
      • 2020-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多