4

I have some code written by me for MS VC++10. I use C++11 and, in particular, expressions like

std::function<int (int)> f =...;
auto it = v.begin();
for_each(it1, it2,[&](int& i) { ++i;}); 

Now I'm trying out MacOS and XCode with llvm&clang and my code can't be compiled! The question is why? Perhaps, I shall specify an option to use c++11. In this case, where can I fix it in xcode?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Kirill Lykov
  • 1,293
  • 2
  • 22
  • 39

2 Answers2

14

You will need Xcode 4.2.

In your build settings, search for "c++0x" and set the "C++ Language Dialect to C++0x [-std=c++0x]". Then search for "libc++" and set the "C++ Standard Library" to "libc++".

Not all C++11 facilities are available. For example lambdas are not yet supported.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • 2
    Many C++11 features are also supported by the Xcode 4.1 clang, but you have to put the options in the 'other flags' fields. Also the Xcode version of clang disallows using libc++ on OS X pre-Lion, so even if you install libc++ you can't use it. Details: http://stackoverflow.com/questions/7226753/osx-lion-xcode-4-1-how-do-i-setup-a-c0x-project/7236451#7236451 – bames53 Nov 02 '11 at 18:14
  • 3
    @bames53: If you want to use libc++ on Snow Leopard, I can help you. Go here (http://libcxx.llvm.org/) and follow the directions for 10.6. You need a helper lib I host on my personal website. You can contact me directly if you need help with this. Xcode 4.1 does not support libc++, but only gcc 4.2. gcc 4.2 gives no library support for C++11 (though there is some tr1 support). – Howard Hinnant Nov 02 '11 at 20:42
5

For a list of C++11 features Clang currently supports, see this nice list. Lambda expressions (and syntactically related initialzer lists) are currently not implemented.

Your only choice for now (until Clang devs implement lambda support) is using MacPorts' GCC 4.5/4.6 compilers.

The extra command-line option would be -std=c++0x (in the next version of Clang and GCC it will be the proper -std=c++11).

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • Note that the list in question are only for what is supported in the repo. What you get from XCode is almost certainly an earlier version. – Nicol Bolas Nov 02 '11 at 18:13
  • Nicol: true enough, but it's easy (and fast) enough to build Clang yourself ;-) – rubenvb Nov 03 '11 at 11:56