【问题标题】:can't compile c++ successfully on macos无法在 macos 上成功编译 c++
【发布时间】:2022-03-04 15:15:53
【问题描述】:

我的 MacOS 版本是 12.2.1 和 sdk 版本是 12.1 我之前可以成功构建它, 但现在在我完成升级我的 xcode 后它失败了。

这是一些错误信息。

In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/vector:276:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/__bit_reference:15:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/algorithm:653:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/memory:676:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/iterator:419:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/__functional_base:80:16: error: no template named \'unary_function\'; did you mean \'binary_function\'?
        static unary_function<_Ap, _Rp>
               ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/__functional_base:27:29: note: \'binary_function\' declared here
struct _LIBCPP_TEMPLATE_VIS binary_function
                            ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/__functional_base:80:16: error: too few template arguments for class template \'binary_function\'
        static unary_function<_Ap, _Rp>
               ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/__functional_base:27:29: note: template is declared here
struct _LIBCPP_TEMPLATE_VIS binary_function
                            ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/__functional_base:81:31: error: no template named \'unary_function\'; did you mean \'binary_function\'?
        __test(const volatile unary_function<_Ap, _Rp>*);
                              ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/__functional_base:27:29: note: \'binary_function\' declared here
struct _LIBCPP_TEMPLATE_VIS binary_function
                            ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/__functional_base:81:31: error: too few template arguments for class template \'binary_function\'
        __test(const volatile unary_function<_Ap, _Rp>*);
                              ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/__functional_base:27:29: note: template is declared here
struct _LIBCPP_TEMPLATE_VIS binary_function
                            ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/__functional_base:168:14: error: no template named \'unary_function\'; did you mean \'binary_function\'?
    : public unary_function<_A1, _Rp>
             ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/__functional_base:27:29: note: \'binary_function\' declared here
struct _LIBCPP_TEMPLATE_VIS binary_function


我的 cmake 配置是这样的


cmake_minimum_required(VERSION 3.0)
project(mytest VERSION 1.0.0)
cmake_policy(SET CMP0074 NEW)
set(CMAKE_CXX_STANDARD 11)
add_compile_options(-stdlib=libc++)


  • 你有 \'include <functional>` 吗?
  • 是的,我愿意,我包括&lt;functional&gt;

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


【解决方案1】:

默认情况下,实际 xcode 提供的 clang 支持 C++17。

std::unary_function 在 C++17 中被删除。 std::function 应改为使用。

set(CMAKE_CXX_STANDARD 11) 太晚了。应该放在project(mytest VERSION 1.0.0)之前,否则默认标准C++17设置为完美。

【讨论】:

  • 他们真的破坏了对 c++11 的支持吗?如果是这样,那比他们仍然默认为 c++98 还要糟糕
猜你喜欢
  • 2021-07-30
  • 2011-08-31
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 2021-03-17
  • 1970-01-01
相关资源
最近更新 更多