c++ -std=c++11 -stdlib=libc++ 在 osx lion 上给出错误

标签 c++ macos osx-lion llvm llvm-3.0

这应该是一个重复的问题。但我在谷歌上搜索了很多,没有找到解决我问题的方法。

我正在尝试在 osx lion 上使用 c++11 标准功能,但它不起作用。我相信此功能称为列表初始值设定项。根据 http://clang.llvm.org/cxx_status.html此功能在 clang 3.1 中,这是我正在使用的版本。

下面是解释我的简单测试的 shell o/p。谁能告诉我我做错了什么?我是不是忘记了一面旗帜?

编译器是:

: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++

操作系统是

: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

测试文件是

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

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

编译器输出为

: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.

好吧,我不想去那里,因为以下内容需要很多时间才能在 stackoverflow 上正确格式化。但是添加 -stdlib=c++ 会产生更多问题。现在我得到 14 个错误。

: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.

最佳答案

-stdlib=libc++ 添加到您的编译器标志。参见 this post寻求解释。

关于c++ -std=c++11 -stdlib=libc++ 在 osx lion 上给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14263735/

相关文章:

c++ - 如何在 C++ 中匹配正则表达式中的\0 字符?

c++ - 发布获取语义以计算平均值

C++:创建字符串

macos - 在终端中设置颜色会导致奇怪的字符行限制

python - Vim :colorscheme on Python

c++ - 为 C++ 类创建最小的可发布头文件

python - Mac OS X Lion Psycopg2 : Symbol not found: _PQbackendPID

c++ -/usr/bin/ld : cannot find : No such file or directory

macos - 在 mac os 上运行 azure 时 Node.js 异常 201

ios - CoreAudio 的首选语言