pro*_*eek 12 c++ lambda clang c++11
我有这个使用lambda的C++ 11代码,这是一个例子.
#include <iostream>
using namespace std;
int main()
{
auto func = [] () { cout << "Hello world"; };
func(); // now call the function
}
Run Code Online (Sandbox Code Playgroud)
当我用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"; };
Run Code Online (Sandbox Code Playgroud)
可能有什么问题?在这个站点中,lambda似乎支持clang 3.1.
使用-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
Run Code Online (Sandbox Code Playgroud)
bit*_*tek 20
这是因为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 ++)编译您的程序,或者您的Clang安装没有链接到libc或libstdc ++.尝试链接每个库以查看哪个适合您,可能没有在您的系统上安装libc.
尝试使用clang ++可执行文件(C++编译器)使用C++ 11模式编译程序,并将其与Clang C++标准库或GNU标准C++库链接
1)
# Uses Clang C++ Library and enables C++11 mode
clang++ -stdlib=libc++ -std=c++11 [input]
Run Code Online (Sandbox Code Playgroud)
2)
# Uses GNU Standard C++ Library and enables C++11 mode
clang++ -stdlib=libstdc++ -std=c++11 [input]
Run Code Online (Sandbox Code Playgroud)
另一个可能的问题可能是您没有使用正确的选项编译Clang以启用C++ 11语言扩展,请在配置Clang的编译过程时尝试检查文档以获取正确的标志.
归档时间: |
|
查看次数: |
8625 次 |
最近记录: |