clang支持C ++ 11 lambda [英] clang's support of C++ 11 lambda

查看:333
本文介绍了clang支持C ++ 11 lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  #include< iostream> 

using namespace std;

int main()
{
auto func = [](){cout< 你好,世界; };
func(); //现在调用函数
}



当我编译这个代码用clang 3.1 code> Apple clang版本3.1(标签/ Apple / clang-318.0.54)(基于LLVM 3.1svn)),我收到此错误

  lambda.cpp:7:17:error:预期表达式
auto func = [](){cout< 你好,世界; };

可能有什么问题?在此网站中,lambda似乎支持clang 3.1。



< h2> ADDED

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

  0。程序参数:/ 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:当前解析器令牌';'
2. lambda.cpp:6:1:解析函数体'main'
3. lambda.cpp:6:1:在复合语句('{}')
clang:error :无法执行命令:分段故障:11
clang:error:clang frontend命令由于信号2失败(使用-v查看调用)
clang:note:diagnostic msg:请提交错误报告到http://developer.apple.com/bugreporter/并包括命令行参数和所有诊断信息。
clang:note:diagnostic msg:预处理源和相关的运行脚本位于:
clang:note:diagnostic msg:/ var / folders / ng / h2hkycqd2q5g2hz42c47bt4w0000gn / T / lambda -roTwCZ.ii
clang:note:diagnostic msg:/var/folders/ng/h2hkycqd2q5g2hz42c47bt4w0000gn/T/lambda-roTwCZ.sh


解决方案

这是因为 clang ++ 默认情况下使用ISO C ++ 1998标准(包括ISO C ++ 2003标准中定义的缺陷)编译代码,除了export(已在C ++ 11中删除)



Lambdas是 Clang的的一部分C++11语言扩展,因此您需要使用 -std = c ++ 11 -std = gnu ++ 11



另请参阅: Clang 3.1和C ++ 11支持状态在Clang中激活C ++ 11支持



编辑:我想你正试图用C编译器编译程序 clang ),而不是C ++编译器( clang ++ libc libstdc ++ 。尝试链接到每个库以查看哪个库适合您,可能是您的系统上可能没有安装libc。



尝试使用C + +11模式下使用 clang ++ 可执行文件(C ++编译器),并将其与 Clang C ++标准库 GNU标准C ++库



1)

 #使用Clang C ++库并启用C ++ 11模式
clang ++ -stdlib = libc ++ -std = c ++ 11 [input]

2)

 code>#使用GNU标准C ++库并启用C ++ 11模式
clang ++ -stdlib = libstdc ++ -std = c ++ 11 [input]
pre>

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


I have this C++ 11 code that uses lambda, this is an example.

#include <iostream>

using namespace std;

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

When I compiled this code with clang 3.1 (Apple clang version 3.1 (tags/Apple/clang-318.0.54) (based on LLVM 3.1svn)), I got this error

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

What might be wrong? In this site, lambda seems to be supported with clang 3.1.

ADDED

With -std=gnu++11 or c++11 option, I got these error messages.

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

解决方案

This is because 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)

Lambdas are part of Clang's C++11 Language Extension, therefore you need to compile your code with -std=c++11 or -std=gnu++11

Also see: Clang 3.1 and C++11 support status and Activating C++11 support in Clang

EDIT: I think you are trying to compile your program with the C compiler (clang) rather than C++ compiler (clang++) or your installation of Clang doesn't link to libc or libstdc++. Try to link against each library to see which one works for you, it is possible that libc might not be installed on your system.

Try to compile your program with C++11 mode using the clang++ executable (the C++ compiler) and link it either with Clang C++ Standard Library or the GNU 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]

Another possible problem might be that you haven't compiled Clang with the right options to enable C++11 language extensions, try and check the documentation for correct flags to use when you configure the compilation process for Clang.

这篇关于clang支持C ++ 11 lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆