Is there a way to enable debug mode in a prebuilt libc++ 15 on either macOS or Ubuntu?

138 views Asked by At

Optional context

Consider the following code with a clearly incorrect comparator:

#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;
    });
}

When compiled with GCC's libstdc++'s -D_GLIBCXX_DEBUG flag (Ubuntu/Windows msys2) or with Visual Studio's debug mode (e.g. /MTd), some internal assertion fails inside std::sort to highlight the 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> >;
    }

A similar -D_LIBCPP_DEBUG flag worked for libc++ version 14 both on Ubuntu (LLVM) and macOS (Apple Clang). See e.g. Godbolt yielding the following assertion failure:

/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

Since version 15 it seems the be not the case anymore, it yields a compilation error:

...
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.

Problem root cause

It seems to be an explicit change by LLVM in https://reviews.llvm.org/D122941: they claim the debug mode never worked in the first place: everyone had to link with the debug version of libc++, but never did. So, since libc++ 15 _LIBCPP_DEBUG no longer compiles unless one links with a special version of libc++.

Question

How do I link with the debug version of libc++ on either Ubuntu 22.04 or macOS? I currently use -stdlib=libc++ on Ubuntu and the default Apple Clang on macOS.

I'd prefer to avoid building libc++ from scratch as it is in an educational setting.

0

There are 0 answers