componentkit icon indicating copy to clipboard operation
componentkit copied to clipboard

xcode 26编译报错C++

Open yangmingxin12138 opened this issue 5 months ago • 2 comments

xcode版本26 MAC OS 26.0 (25A354) 编译有C++语法报错

Image

yangmingxin12138 avatar Sep 29 '25 07:09 yangmingxin12138

我也遇到报错

Image 以下回答来自ChatGPT:

先别被“系统头文件报错”吓到。 这是标准库在模板实例化时帮你把真正的错误“炸”出来了。

结论(核心原因)

你的某处代码(来自 ComponentKit 的 TextKit 渲染模块)把 const T 当成了容器的元素类型或 allocator 的 value_type,导致实例化出:

std::allocator<const CK::TextKit::Renderer::Key>
std::vector<const CK::TextKit::Renderer::Key>

C++ 标准明确禁止std::allocator<T>T 不能是 const/volatile/void。 Xcode 新版 libc++ 在 allocator_traits 里加了 static_assert,所以错误点显示在 __memory / allocator_traits.h,但锅在上层代码

左侧错误也印证了这一点:

  • std::allocator does not support const types
  • “No type named 'value_type' in 'std::allocator<const ...>'”
  • enable_if 相关报错是连锁反应。

怎么修

1) 修源头:别把 const T 放进容器/allocator

Pods/ComponentKit 里把使用到 Renderer::Key 的容器从 const Key 改成 Key。 示例(伪):

// ❌ 这样会触发你现在的断言
std::vector<const CK::TextKit::Renderer::Key> keys;

// ✅ 改成非 const 元素
std::vector<CK::TextKit::Renderer::Key> keys;

// 如果需要只读语义,用 const 修饰变量而不是模板参数:
const std::vector<CK::TextKit::Renderer::Key> keys_ro = ...;

// 或者保存指针/智能指针来表达只读:
std::vector<std::shared_ptr<const CK::TextKit::Renderer::Key>> keys_ptrs;

定位办法(在工程根目录执行):

grep -RIn "Renderer::Key" Pods/ComponentKit | grep -E "vector|set|map|unordered|allocator|value_type"

把命中处的 const CK::TextKit::Renderer::Key 全部改为 CK::TextKit::Renderer::Key

语义不变:容器里放“可变类型”的值,然后通过 const 容器、span<const T>、指针的 const 来保证只读。

2) 临时补丁(不 fork,也不手改 Pods)

Podfile 里加一个 post_install patch(安装后自动替换):

post_install do |installer|
  Dir.glob('Pods/ComponentKit/**/*.{h,hpp,hh,mm,cc,cpp}').each do |f|
    text = File.read(f)
    newt = text.gsub('const CK::TextKit::Renderer::Key', 'CK::TextKit::Renderer::Key')
    if newt != text
      File.open(f, 'w'){ |w| w.write(newt) }
      puts "Patched const Key in: #{f}"
    end
  end
end

然后:

pod deintegrate
pod install
rm -rf ~/Library/Developer/Xcode/DerivedData/*

重新编译。

3) 更长远的方案

  • 升级 ComponentKit 到**兼容 Xcode/SDK 新版 libc++**的版本(很多库在较新 SDK 下都做过这类修复)。
  • 如果你自己在项目里也有类似写法(比如 std::map<Key, Value, ..., std::allocator<const Key>>std::vector<const T>),一并按上面的规则改掉。

为什么会“突然”炸

旧版 libc++ 对这类未定义/不合规用法没做硬性断言,新版把标准里的 allocator 要求写成了 static_assert,于是错误看起来像“系统文件报错”。其实是它在替你抓真正的源头


快速检查清单

  • [ ] 工程 / Pods 里是否出现了 std::vector<const T>std::allocator<const T>std::map<const T, …> 等写法
  • [ ] 清掉 DerivedData 后重编
  • [ ] 若用的是 fork 的 ComponentKit,合并官方修复或自行提交一个 PR
  • [ ] C++ 语言级别设为 C++17/20 均可,但无论哪个标准allocator<const T> 都是不允许的

有路径或具体文件名我也可以直接给出精确的 diff。

JobsKits avatar Nov 11 '25 02:11 JobsKits

我下载后改了些文件就可以了

Image Image Image Image Image

yangmingxin12138 avatar Nov 11 '25 03:11 yangmingxin12138