clang对C ++ 11 lambda的支持

时间:2012-11-12 20:26:35

标签: c++ c++11 lambda clang

我有这个使用lambda的C ++ 11代码,这是一个例子。

#include <iostream>

using namespace std;

int main()
{
    auto func = [] () { cout << "Hello world"; };
    func(); // now call the function
}

当我使用clang 3.1(Apple clang version 3.1 (tags/Apple/clang-318.0.54) (based on LLVM 3.1svn))编译此代码时,我收到此错误

lambda.cpp:7:17: error: expected expression
auto func = [] () { cout << "Hello world"; };

可能有什么问题?在this site中,lambda似乎得到了clang 3.1的支持。

ADDED

使用-std = gnu ++ 11或c ++ 11选项,我收到了这些错误消息。

0.      Program arguments: /usr/bin/clang -cc1 -triple x86_64-apple-macosx10.7.4 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name lambda.cpp -pic-level 1 -mdisable-fp-elim -relaxed-aliasing -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 128.2 -resource-dir /usr/bin/../lib/clang/3.1 -fmodule-cache-path /var/folders/ng/h2hkycqd2q5g2hz42c47bt4w0000gn/T/clang-module-cache -std=gnu++11 -fdeprecated-macro -fdebug-compilation-dir /Users/smcho/Desktop/C++test -ferror-limit 19 -fmessage-length 173 -stack-protector 1 -fblocks -fobjc-runtime-has-arc -fobjc-runtime-has-weak -fobjc-dispatch-method=mixed -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/ng/h2hkycqd2q5g2hz42c47bt4w0000gn/T/lambda-XvZzHg.o -x c++ lambda.cpp 
1.      lambda.cpp:7:49: current parser token ';'
2.      lambda.cpp:6:1: parsing function body 'main'
3.      lambda.cpp:6:1: in compound statement ('{}')
clang: error: unable to execute command: Segmentation fault: 11
clang: error: clang frontend command failed due to signal 2 (use -v to see invocation)
clang: note: diagnostic msg: Please submit a bug report to http://developer.apple.com/bugreporter/ and include command line arguments and all diagnostic information.
clang: note: diagnostic msg: Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /var/folders/ng/h2hkycqd2q5g2hz42c47bt4w0000gn/T/lambda-roTwCZ.ii
clang: note: diagnostic msg: /var/folders/ng/h2hkycqd2q5g2hz42c47bt4w0000gn/T/lambda-roTwCZ.sh

4 个答案:

答案 0 :(得分:19)

这是因为 clang ++ by default compiles your code using ISO C++ 1998 standard (including the defects addressed in the ISO C++ 2003 standard) except for 'export' (which has been removed in C++11)

Lambda属于Clang's C++11 Language Extension的一部分,因此您需要使用 -std = c ++ 11 -std = gnu ++编译代码11

另请参阅:Clang 3.1 and C++11 support statusActivating C++11 support in Clang

编辑:我认为您正在尝试使用C编译器( clang )而不是C ++编译器( clang ++ )或您的Clang安装未链接到 libc libstdc ++ 。尝试链接每个库以查看哪个适合您,可能没有在您的系统上安装libc。

尝试使用 clang ++ 可执行文件(C ++编译器)使用C ++ 11模式编译程序,并将其链接到Clang C++ Standard LibraryGNU Standard C++ Library

1)

# Uses Clang C++ Library and enables C++11 mode
clang++ -stdlib=libc++ -std=c++11 [input] 

2)

# Uses GNU Standard C++ Library and enables C++11 mode
clang++ -stdlib=libstdc++ -std=c++11 [input]

另一个可能的问题可能是您没有使用正确的选项编译Clang以启用C ++ 11语言扩展,请尝试检查文档以获取在为Clang配置编译过程时使用的正确标志。

答案 1 :(得分:3)

使用AppStore更新Xcode,但它仍然在我的Mac上崩溃(Lion 10.7.5)

我可以下载macport来成功编译示例。

sudo port install clang-3.1
clang++-mp-3.1 -std=c++11 lambda.cpp 

答案 2 :(得分:2)

回应新编辑的帖子:

我对此问题进行了一些调查,这是一个在clang 3.1的发布版本中得到纠正的错误。我目前正在使用:

Debian clang version 3.1-3eudoxos1 (branches/release_31) (based on LLVM 3.1)

然而,当我使用clang 3.0-6ubuntu3进行测试时,我收到与您发布的错误类似的错误。

因为您的版本是从SVN标记的,所以我认为您的问题是它来自3.1的预发行版本,而lambda支持还没有完全实现。

我可以确认lambdas在clang中工作,因为我正在开发一个使用它们的项目,我们同时针对clang和gcc。有一些编译器不时出现;但是,当然没有像你给出的例子那么简单。

所以,我的建议是更新你的clang版本。

答案 3 :(得分:0)

我需要安装命令行工具,就像这篇文章解释的那样 - Does Xcode 4.4 come with subversion?

enter image description here