简体   繁体   中英

clang's support of C++ 11 lambda

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.

The Xcode is updated using AppStore, but it still crashes on my Mac (Lion 10.7.5)

I could download the macport to compile the example successfully.

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

Responding to the newly edited post:

I investigated this issue a bit, and it's a bug that was corrected in release versions of clang 3.1 . I'm currently using:

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

However when I tested with clang 3.0-6ubuntu3 I get a similar error to the one you posted.

Because your version is marked from SVN I assume that your issue is that it's from the prerelease versions of 3.1 and lambda support hadn't been fully implemented yet.

I can confirm that lambdas do work in clang because I'm working on a project that uses them and we target both clang and gcc. There are a few compiler boogs that crop up from time to time; however, nothing as simple as your given example, of course.

So, my recommendation is to update your version of clang.

I needed to install command line tools as is this post explains - Does Xcode 4.4 come with subversion?

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM