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

如何在Ubuntu 22.04或macOS上链接libc++15调试版本?

可选上下文

考虑以下带有明显错误比较器的代码:

#include <algorithm>
#include <vector>

int main() {
    std::vector<int> a{3, 1, 2, 4};
    sort(a.begin(), a.end(), [](int a, int b) {
        return true;
    });
}

使用GCC的libstdc++的-D_GLIBCXX_DEBUG编译选项(Ubuntu/Windows msys2环境),或Visual Studio的调试模式(如/MTd)时,std::sort内部会触发断言失败,以暴露UB:

/usr/include/c++/12/debug/vector:442:
In function:
    std::debug::vector<_Tp, _Allocator>::reference std::debug::vector<_Tp, 
    _Allocator>::operator[](size_type) [with _Tp = int; _Allocator = 
    std::allocator<int>; reference = int&; size_type = long unsigned int]

Error: attempt to subscript container with out-of-bounds index 0, but 
container only holds 0 elements.

Objects involved in the operation:
    sequence "this" @ 0x7fffea1ed010 {
      type = std::debug::vector<int, std::allocator<int> >;;
    }

类似的_LIBCPP_DEBUG编译选项在libc++ 14版本中(Ubuntu的LLVM、macOS的Apple Clang环境)可以正常工作,例如会触发如下断言失败:

/opt/compiler-explorer/clang-14.0.0/bin/../include/c++/v1/__algorithm/comp_ref_type.h:61: _LIBCPP_ASSERT '!__comp_(__l, __r)' failed. Comparator does not induce a strict weak ordering
Program terminated with signal: SIGSEGV

但从libc++ 15版本开始,使用该选项会导致编译错误:

...
In file included from /usr/lib/llvm-15/bin/../include/c++/v1/algorithm:1709:
/usr/lib/llvm-15/bin/../include/c++/v1/__debug:24:5: error: "Enabling the debug mode now requires having configured the library with support for the debug mode"
#   error "Enabling the debug mode now requires having configured the library with support for the debug mode"
    ^
1 error generated.

问题根源

这是LLVM的一项明确修改:他们认为此前的调试模式从未正常工作,用户必须链接libc的调试版本但并未这么做。因此从libc 15开始,_LIBCPP_DEBUG选项仅在链接特殊调试版本的libc++时才能编译通过。

技术问询

我当前在Ubuntu上使用-stdlib=libc++,在macOS上使用默认的Apple Clang,请问如何在Ubuntu 22.04或macOS系统上链接libc++ 15的调试版本?由于是教学场景,我希望避免从头编译libc++源码。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 15:32:21