【问题标题】:Using clang with libc++ and c++11 from the command line with mac os x在 mac os x 的命令行中将 clang 与 libc++ 和 c++11 一起使用
【发布时间】:2012-12-06 20:45:49
【问题描述】:

如果我启用 c++11 和 libc++,我什至无法使用 clang 编译一个简单的 hello world。比如下面的代码

#include <iostream>

int main()
{
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

当我运行 clang++ -std=c++11 -stdlib=libc++ main.cpp 时从编译中产生以下输出

In file included from main.cpp:1:
In file included from /usr/bin/../lib/c++/v1/iostream:38:
In file included from /usr/bin/../lib/c++/v1/ios:216:
In file included from /usr/bin/../lib/c++/v1/__locale:15:
In file included from /usr/bin/../lib/c++/v1/string:434:
In file included from /usr/bin/../lib/c++/v1/algorithm:591:
/usr/bin/../lib/c++/v1/type_traits:165:12: error: unknown type name
      '_LIBCPP_CONSTEXPR'
    static _LIBCPP_CONSTEXPR const _Tp      value = __v;
           ^
/usr/bin/../lib/c++/v1/type_traits:165:30: error: expected member name or ';'
      after declaration specifiers
    static _LIBCPP_CONSTEXPR const _Tp      value = __v;
    ~~~~~~~~~~~~~~~~~~~~~~~~ ^
/usr/bin/../lib/c++/v1/type_traits:169:9: error: unknown type name
      '_LIBCPP_CONSTEXPR'
        _LIBCPP_CONSTEXPR operator value_type() const {return value;}
        ^
/usr/bin/../lib/c++/v1/type_traits:169:63: error: use of undeclared identifier
      'value'
        _LIBCPP_CONSTEXPR operator value_type() const {return value;}
                                                              ^
/usr/bin/../lib/c++/v1/type_traits:173:1: error: unknown type name
      '_LIBCPP_CONSTEXPR'
_LIBCPP_CONSTEXPR const _Tp integral_constant<_Tp, __v>::value;
^
/usr/bin/../lib/c++/v1/type_traits:173:19: error: expected unqualified-id
_LIBCPP_CONSTEXPR const _Tp integral_constant<_Tp, __v>::value;
                  ^
/usr/bin/../lib/c++/v1/type_traits:730:2: error: is_base_of not implemented.
#error is_base_of not implemented.
 ^
/usr/bin/../lib/c++/v1/type_traits:400:56: error: no member named 'value' in
      'std::__1::is_integral<void>'
    : public integral_constant<bool, is_integral<_Tp>::value      ||
                                     ~~~~~~~~~~~~~~~~~~^
/usr/bin/../lib/c++/v1/type_traits:941:19: note: in instantiation of template
      class 'std::__1::is_arithmetic<void>' requested here
                 (is_arithmetic<_A2>::value || is_void<_A2>::value) &&
                  ^
/usr/bin/../lib/c++/v1/type_traits:949:22: note: in instantiation of default
      argument for '__promote<type-parameter-0-0, void, void>' required here
    typedef typename __promote<_A1>::type __type1;
                     ^~~~~~~~~~~~~~
/usr/bin/../lib/c++/v1/type_traits:949:38: error: expected a qualified name
      after 'typename'
    typedef typename __promote<_A1>::type __type1;
                                     ^
/usr/bin/../lib/c++/v1/type_traits:949:42: error: expected ';' at end of
      declaration list
    typedef typename __promote<_A1>::type __type1;
                                         ^
/usr/bin/../lib/c++/v1/type_traits:941:39: error: no member named 'value' in
      'std::__1::is_arithmetic<void>'
                 (is_arithmetic<_A2>::value || is_void<_A2>::value) &&
                  ~~~~~~~~~~~~~~~~~~~~^
/usr/bin/../lib/c++/v1/type_traits:950:22: note: in instantiation of default
      argument for '__promote<type-parameter-0-1, void, void>' required here
    typedef typename __promote<_A2>::type __type2;
                     ^~~~~~~~~~~~~~
/usr/bin/../lib/c++/v1/type_traits:950:38: error: expected a qualified name
      after 'typename'
    typedef typename __promote<_A2>::type __type2;
                                     ^
/usr/bin/../lib/c++/v1/type_traits:950:42: error: expected ';' at end of
      declaration list
    typedef typename __promote<_A2>::type __type2;
                                         ^
/usr/bin/../lib/c++/v1/type_traits:941:39: error: no member named 'value' in
      'std::__1::is_arithmetic<void>'
                 (is_arithmetic<_A2>::value || is_void<_A2>::value) &&
                  ~~~~~~~~~~~~~~~~~~~~^
/usr/bin/../lib/c++/v1/type_traits:951:38: error: expected a qualified name
      after 'typename'
    typedef typename __promote<_A3>::type __type3;
                                     ^
/usr/bin/../lib/c++/v1/type_traits:951:42: error: expected ';' at end of
      declaration list
    typedef typename __promote<_A3>::type __type3;
                                         ^
/usr/bin/../lib/c++/v1/type_traits:953:22: error: use of undeclared identifier
      '__type1'
    typedef decltype(__type1() + __type2() + __type3()) type;
                     ^
/usr/bin/../lib/c++/v1/type_traits:953:34: error: use of undeclared identifier
      '__type2'
    typedef decltype(__type1() + __type2() + __type3()) type;
                                 ^
/usr/bin/../lib/c++/v1/type_traits:953:46: error: use of undeclared identifier
      '__type3'
    typedef decltype(__type1() + __type2() + __type3()) type;
                                             ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

如果我使用 xcode 并设置 libc++ 和 c++11 支持的选项,代码将编译得很好。

感谢您的回复

【问题讨论】:

  • clang++ -v 说什么?
  • Apple clang version 4.0 (tags/Apple/clang-421.0.60) (based on LLVM 3.1svn) Target: x86_64-apple-darwin11.4.2 Thread model: posix
  • Clang 4.0 已过时。你应该有4.1。你安装的是什么版本的 Xcode?​​span>
  • 我好像有版本 4.5.2 (4G2008a)
  • 进入 Xcode 首选项,选择“下载”窗格,然后查看“组件”选项卡。应该有一个“命令行工具”条目。有“安装”按钮吗?如果是,请按下它。

标签: c++ macos c++11 clang libc++


【解决方案1】:

您的命令行工具已过时。 Xcode 4.4 附带 Clang 4.0。您需要 Xcode 4.5 附带的 Clang 4.1。安装 Xcode 后,您可以转到 Preferences 窗口,选择 Downloads 窗格和 Components 选项卡,然后有一个“Command Line Tools”条目。右侧会有一个按钮,上面写着“安装”。

【讨论】:

    猜你喜欢
    • 2013-05-15
    • 2014-02-04
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 2012-06-08
    • 2011-12-11
    相关资源
    最近更新 更多