我有这个使用 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"; };
可能出了什么问题?在这个网站 http://clang.llvm.org/cxx_status.html,clang 3.1 似乎支持 lambda。
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
这是因为clang++ 默认情况下,使用 ISO C++ 1998 标准(包括 ISO C++ 2003 标准中解决的缺陷)编译代码,“导出”除外(已在 C++11 中删除) http://clang.llvm.org/cxx_status.html
Lambda 是以下的一部分Clang's http://clang.llvm.org/ C++11 语言扩展 http://clang.llvm.org/docs/LanguageExtensions.html#cxx11,因此你需要编译你的代码-std=c++11 or -std=gnu++11
另请参阅:Clang 3.1 和 C++11 支持状态 https://stackoverflow.com/questions/10601545/clang-3-1-and-c11-support-status and 在 Clang 中激活 C++11 支持 https://stackoverflow.com/questions/7875241/activating-c11-support-in-clang#comment9616450_7875241
编辑:我认为你正在尝试使用 C 编译器编译你的程序(clang)而不是 C++ 编译器(clang++)或者您安装的 Clang 没有链接到libc or libstdc++ 库。尝试链接每个库,看看哪一个适合您,您的系统上可能没有安装 libc。
尝试使用 C++11 模式编译您的程序clang++可执行文件(C++编译器)并将其链接到Clang C++ 标准库 http://libcxx.llvm.org/ or the GNU 标准 C++ 库 http://gcc.gnu.org/libstdc++/
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 的编译过程时使用正确的标志。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)