Enabling C++11

I got a clang LLVM 1.0 error when compiling C++ Lambda with Xcode 4.3.3. It seems Xcode 4.3 doesn’t support it although the latest clang supports it. So, I installed gcc4.7 which supports it.

sudo port install gcc_select
sudo port install gcc47
sudo port select gcc mp-gcc47

It worked in a terminal, but didn’t work well with Eclipse which needed the following settings.

1. Right click on your project -> properties -> C/C++ build -> Environment -> add "PATH = /opt/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:".
2. project -> properties -> C/C++ Build -> Tool Chain Editor -> Choose Linux GCC.
3. project -> properties -> C/C++ Build -> Settings -> Tool Settings -> GCC C++ Compiler -> Miscellaneous -> Tool Sttings -> add "-std=c++11"


One more thing, but I don’t know how to resolve the following false positive errors although compile succeeds.

Finally I can experiment C++11 features. I began with Lambda, auto, range-based for loop.

void lamda_test()
{
   auto f = [](){};
   f(); // do nothing

   int ar[] = {1, 2, 3, 4, 5};
   auto f2 = [=]() { for(auto& i:ar) { cout << i << endl;} };	// capture ar (value)
   f2(); // print 1,2,3,4,5

   vector<int> v;
   for (int i = 0; i < 10; i++) {
       v.push_back(i);
   }
   int sum = 0;
   for_each(v.begin(), v.end(), [&](int x) { sum += x; }); // capture sum by '&'(reference)
   cout << sum << endl; // print 45
}

Leave a Reply

Your email address will not be published. Required fields are marked *