Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 259
prosseek Avatar asked Nov 12 '12 20:11

prosseek


People also ask

What is lambda expression in C ++ 11?

In C++11 and later, a lambda expression—often called a lambda—is a convenient way of defining an anonymous function object (a closure) right at the location where it's invoked or passed as an argument to a function.

How lambda function works internally in C++?

Lambda expression in C++ Generally return-type in lambda expression are evaluated by compiler itself and we don't need to specify that explicitly and -> return-type part can be ignored but in some complex case as in conditional statement, compiler can't make out the return type and we need to specify that.

Does C have lambda?

the C standard does not define lambdas at all but the implementations can add extensions. Gcc also added an extension in order for the programming languages that support lambdas with static scope to be able to convert them easily toward C and compile closures directly.


2 Answers

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.

like image 90
Alex Bitek Avatar answered Sep 18 '22 08:09

Alex Bitek


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 
like image 20
prosseek Avatar answered Sep 22 '22 08:09

prosseek