3

This ought to be a duplicate question. But I've googled quite a bit and haven't found a hit that fixes my issue.

I'm trying to use a c++11 standard feature on osx lion and it's not working. I believe this feature is called list initializers. according to http://clang.llvm.org/cxx_status.html This feature was in clang 3.1, which is the version I'm using.

Below is the shell o/p explaining my simple test. Can anyone tell me what I'm doing wrong? Am I forgetting a flag?

The compiler is:

:tmp$ c++ --version
Apple clang version 3.1 (tags/Apple/clang-318.0.61) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.4.2
Thread model: posix
:tmp$ which c++
/usr/bin/c++
:tmp$ ls -alt /usr/bin/c++
lrwxr-xr-x  1 root  wheel  7 Jul 12 13:17 /usr/bin/c++ -> clang++

The OS is

:tmp$ uname -a
Darwin .local 11.4.2 Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64 x86_64

The test file is

:tmp$ cat test.cpp
#include <vector>

int main(){
  std::vector<int> vec1 {1,2,3};
  std::vector<int> vec2={1,2,3};
}

The compiler output is

:tmp$ c++ -std=c++11 test.cpp
 test.cpp:4:20: error: non-aggregate type 'std::vector<int>' cannot be
      initialized with an initializer list
  std::vector<int> vec1 {1,2,3};
                   ^    ~~~~~~~
test.cpp:5:20: error: non-aggregate type 'std::vector<int>' cannot be
      initialized with an initializer list
  std::vector<int> vec2={1,2,3};
                   ^    ~~~~~~~
2 errors generated.

Ok I didn't want to get there because the following takes a lot to format properly on stackoverflow. But adding -stdlib=c++ creates even more problems. Now I get 14 errors.

:tmp$ c++ -std=c++11 -stdlib=libc++ test.cpp 
In file included from test.cpp:1:
In file included from /usr/bin/../lib/c++/v1/vector:261:
In file included from /usr/bin/../lib/c++/v1/__bit_reference:15:
In file included from /usr/bin/../lib/c++/v1/algorithm:591:
/usr/bin/../lib/c++/v1/type_traits:737:2: error: #error is_base_of not
      implemented.
#error is_base_of not implemented.
 ^
/usr/bin/../lib/c++/v1/type_traits:1700:13: error: use of undeclared identifier
      'is_base_of'
            is_base_of<_Class, typename remove_reference<_Tp>::type>::value>
            ^
/usr/bin/../lib/c++/v1/type_traits:1700:24: error: '_Class' does not refer to a
      value
            is_base_of<_Class, typename remove_reference<_Tp>::type>::value>
                   ^
/usr/bin/../lib/c++/v1/type_traits:1697:28: note: declared here
template <class _Rp, class _Class, class _Tp>
                           ^
/usr/bin/../lib/c++/v1/type_traits:1700:62: error: expected class name
            is_base_of<_Class, typename remove_reference<_Tp>::type>::value>
                                                             ^
In file included from test.cpp:1:
In file included from /usr/bin/../lib/c++/v1/vector:261:
In file included from /usr/bin/../lib/c++/v1/__bit_reference:15:
In file included from /usr/bin/../lib/c++/v1/algorithm:594:
In file included from /usr/bin/../lib/c++/v1/memory:590:
In file included from /usr/bin/../lib/c++/v1/typeinfo:61:
/usr/bin/../lib/c++/v1/exception:194:20: error: use of undeclared identifier
      'is_base_of'
                  !is_base_of<nested_exception, typename ...
               ^
/usr/bin/../lib/c++/v1/exception:194:31: error: 'nested_exception' does not
      refer to a value
                  !is_base_of<nested_exception, typename ...
                              ^
/usr/bin/../lib/c++/v1/exception:166:29: note: declared here
class _LIBCPP_EXCEPTION_ABI nested_exception
                            ^
/usr/bin/../lib/c++/v1/exception:194:81: error: parameter declarator cannot be
      qualified
  ...typename remove_reference<_Tp>::type>::value
                                   ~~^
/usr/bin/../lib/c++/v1/exception:194:85: error: expected ')'
  ...typename remove_reference<_Tp>::type>::value
                                         ^
/usr/bin/../lib/c++/v1/exception:192:18: note: to match this '('
throw_with_nested(_Tp&& __t, typename enable_if<
                 ^
/usr/bin/../lib/c++/v1/exception:213:19: error: use of undeclared identifier
      'is_base_of'
                  is_base_of<nested_exception, typename ...
              ^
/usr/bin/../lib/c++/v1/exception:213:30: error: 'nested_exception' does not
      refer to a value
                  is_base_of<nested_exception, typename ...
                         ^
/usr/bin/../lib/c++/v1/exception:166:29: note: declared here
class _LIBCPP_EXCEPTION_ABI nested_exception
                            ^
/usr/bin/../lib/c++/v1/exception:213:80: error: parameter declarator cannot be
      qualified
  ...typename remove_reference<_Tp>::type>::value
                                   ~~^
/usr/bin/../lib/c++/v1/exception:213:84: error: expected ')'
  ...typename remove_reference<_Tp>::type>::value
                                         ^
/usr/bin/../lib/c++/v1/exception:211:18: note: to match this '('
throw_with_nested(_Tp&& __t, typename enable_if<
                 ^
test.cpp:4:20: error: non-aggregate type 'std::vector<int>' cannot be
      initialized with an initializer list
  std::vector<int> vec1 {1,2,3};
                   ^    ~~~~~~~
test.cpp:5:20: error: non-aggregate type 'std::vector<int>' cannot be
      initialized with an initializer list
  std::vector<int> vec2={1,2,3};
                   ^    ~~~~~~~
14 errors generated.
Spundun
  • 3,936
  • 2
  • 23
  • 36
  • possible duplicate of [How do I get the new C++ threading support on Mac OS X with clang?](http://stackoverflow.com/questions/14177018/how-do-i-get-the-new-c-threading-support-on-mac-os-x-with-clang) and see http://stackoverflow.com/a/14150421/981959 for a detailed explanation – Jonathan Wakely Jan 10 '13 at 17:50
  • 1
    The second error just seems to be a bug in libc++, it doesn't implement the features you're using. Try upgrading to LLVM/Clang/libc++ 3.2 – Jonathan Wakely Jan 10 '13 at 17:57
  • 1
    And to format a whole block properly just paste it in, then select the whole block and press Ctrl-K (or the **{}** icon above the textbox) – Jonathan Wakely Jan 10 '13 at 17:58
  • I have the latest version of XCode for Lion. Does this mean XCode on Lion is broken? I don't want to upgrade my OS or go down the darwin ports route. – Spundun Jan 10 '13 at 18:32
  • Thanks for the block comment tip also. – Spundun Jan 10 '13 at 18:32
  • 2
    @Spundun - Note that "Apple clang version 3.1" isn't the same as "LLVM Clang 3.1". On Mountain Lion you would get "Apple clang version 4.0", also "based on LLVM 3.1". – Bo Persson Jan 10 '13 at 18:40

3 Answers3

4

Add -stdlib=libc++ to your compiler flags. See this post for an explanation.

Simon
  • 31,675
  • 9
  • 80
  • 92
  • Had already tried that. Not working. See my edit to the question. – Spundun Jan 10 '13 at 17:42
  • I do realize that given the original content of my question, the right answer was to add --stdlib=libc++, eventhough I had already tried it and that by itself was not enough. – Spundun Jan 10 '13 at 19:23
4

The version you're using is:

Apple clang version 3.1 (tags/Apple/clang-318.0.61) (based on LLVM 3.1svn)

Although this says '3.1' it's not actually the same '3.1' referred to by the C++ status page you looked at. The Apple versions and the LLVM.org versions are essentially unrelated.

Apple clang version 3.1 is "based on LLVM 3.1svn" which means it's based on a pre "LLVM.org 3.1" revision in the LLVM.org source code repository and may not have all the features of the final LLVM.org 3.1 release.

Apple clang 3.1 is kind of old now. Since then LLVM.org has produced "3.1" and "3.2" releases, and Apple has produced up to "Apple clang 4.1" I think. You should either update Xcode or see if there's a more recent version of clang installed somewhere else.

bames53
  • 86,085
  • 15
  • 179
  • 244
  • Thanks for the extra info on the versions. Yeah I had to update that through XCode preferences (not software update). And now my clang is upto 4.1. – Spundun Jan 10 '13 at 19:51
3

The Solution

Specifically, I had to go to XCode preferences->Downloads and install "Command Line Tools". That fixed the problem.

The Explanation

What happened was a simple case of outdated software. Since OSX was up-to-date (through software update), I assumed all the XCode and compiler updates are also in place. Which was not the case.

Notice in the comment to the question I said "I have the latest version of XCode for Lion." But I didn't.(bames53 alludes to that in his answer) Just because you don't have any new updates in the OSX software update doesn't mean that you have the latest XCode compiler. You update it through XCode Preferences.

With the help of Jonathan's feedback and some more googling, Using clang with libc++ and c++11 from the command line with mac os x fixed my problem.

This actually updated the llvm toolchain.

$ c++ --version
Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.4.2
Thread model: posix
Community
  • 1
  • 1
Spundun
  • 3,936
  • 2
  • 23
  • 36