You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

clang12编译C++20代码找不到<compare>头文件如何解决

问题原因

从编译添加-v参数的输出可以看到,clang自动识别到的GCC安装路径为GCC 9对应的路径,使用的libstdc版本为9,而<compare>作为C20引入的标准头文件,仅在libstdc++ 10及以上版本提供。即使你单独安装了libstdc++6 10.3版本,clang默认也会优先绑定匹配到的GCC版本对应的标准库,因此无法找到对应头文件。

解决方案

方案一:继续使用libstdc++

  1. 先安装完整的GCC 10工具链:
sudo apt install g++-10
  1. 编译时添加参数手动指定clang使用的GCC工具链路径,修改后的编译命令如下:
clang++-12 ./test/math/ordering.cpp -o ./test/math/ordering.exe -I ./inc/lcu --include testing/assert.h -std=c++20 -stdlib=libstdc++ --gcc-toolchain=/usr/lib/gcc/x86_64-linux-gnu/10/

编译后可再次添加-v参数验证,确认include搜索路径中出现c++/10相关目录即可正常找到<compare>头文件。

方案二:切换使用LLVM自带的libc++标准库

  1. 先安装libc++相关依赖:
sudo apt install libc++-12-dev libc++abi-12-dev
  1. 编译时将stdlib参数改为libc++即可:
clang++-12 ./test/math/ordering.cpp -o ./test/math/ordering.exe -I ./inc/lcu --include testing/assert.h -std=c++20 -stdlib=libc++

内容的提问来源于stack exchange,提问作者lenerdv

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 14:54:03