【问题标题】:c++ -std=c++11 -stdlib=libc++ giving errors on osx lionc++ -std=c++11 -stdlib=libc++ 在 osx lion 上给出错误
【发布时间】:2013-01-10 17:27:17
【问题描述】:

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

我正在尝试在 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.

【问题讨论】:

  • 第二个错误似乎是 libc++ 中的一个错误,它没有实现您正在使用的功能。尝试升级到 LLVM/Clang/libc++ 3.2
  • 要正确格式化整个块,只需将其粘贴进去,然后选择整个块并按 Ctrl-K(或文本框上方的 {} 图标)
  • 我有最新版本的 XCode for Lion。这是否意味着 Lion 上的 XCode 已损坏?我不想升级我的操作系统或走 darwin ports 路线。
  • @Spundun - 请注意,“Apple clang 3.1 版”与“LLVM Clang 3.1”不同。在 Mountain Lion 上,您会得到“Apple clang 4.0 版”,也是“基于 LLVM 3.1”。

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


【解决方案1】:

-stdlib=libc++ 添加到您的编译器标志中。请参阅this post 了解说明。

【讨论】:

  • 已经试过了。不工作。请参阅我对问题的编辑。
  • 我确实意识到,鉴于我的问题的原始内容,正确的答案是添加 --stdlib=libc++,尽管我已经尝试过了,而且仅靠它本身是不够的。
【解决方案2】:

您使用的版本是:

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

虽然上面写的是“3.1”,但实际上与您查看的C++ status page 所指的“3.1”不同。 Apple 版本和 LLVM.org 版本本质上是无关的。

Apple clang 3.1 版“基于 LLVM 3.1svn”,这意味着它基于 LLVM.org 源代码存储库中的“LLVM.org 3.1”预修订版,可能不具备最终 LLVM.org 的所有功能3.1 发布。

Apple clang 3.1 现在有点老了。从那时起,LLVM.org 已经发布了“3.1”和“3.2”版本,我认为 Apple 已经发布了“Apple clang 4.1”。您应该更新 Xcode 或查看其他地方是否安装了更新版本的 clang。

【讨论】:

  • 感谢您提供有关版本的额外信息。是的,我必须通过 XCode 首选项(不是软件更新)来更新它。现在我的clang达到了4.1。
【解决方案3】:

解决方案

具体来说,我必须转到 XCode 首选项-> 下载并安装“命令行工具”。这解决了问题。

解释

发生的只是一个过时软件的简单案例。由于 OSX 是最新的(通过软件更新),我假设所有 XCode 和编译器更新也都已到位。事实并非如此。

请注意在评论中对我所说的“我有最新版本的 XCode for Lion”的问题。但我没有。(bames53 在他的回答中提到了这一点)仅仅因为您在 OSX 软件更新中没有任何新更新并不意味着您拥有最新的 XCode 编译器。您可以通过 XCode Preferences 对其进行更新。

在 Jonathan 的反馈和更多谷歌搜索的帮助下,Using clang with libc++ and c++11 from the command line with mac os x 解决了我的问题。

这实际上更新了 llvm 工具链。

$ 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

【讨论】:

    猜你喜欢
    • 2013-04-29
    • 2012-08-21
    • 2016-12-17
    • 1970-01-01
    • 2014-02-25
    • 2012-12-16
    • 2013-07-01
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多