How to use (link) debug version of libc++ in macOS?
Asked Answered
P

2

7

I want to enable debug version of libc++ in macOS, so I define _LIBCPP_DEBUG=1 in cxx_build_flags [Debug Version of libC++], but unable to link debug function of libc++. I guess there is only release version of libc++ in my system, so how can I get the debug version of libc++ in macOS

Undefined symbols for architecture x86_64:
"std::__1::__libcpp_db::__decrementable(void const*) const", referenced from:
  void std::__1::__nth_element<std::__1::__debug_less<std::__1::__less<float, float> >&, std::__1::__wrap_iter<float*> >(std::__1::__wrap_iter<float*>, std::__1::__wrap_iter<float*>, std::__1::__wrap_iter<float*>, std::__1::__debug_less<std::__1::__less<float, float> >&)
Putrescible answered 27/3, 2018 at 8:37 Comment(4)
Why do you want to use a debug-build of the standard library? What is the actual problem you want to solve?Gilemette
I just want to enable assert function(such as index check in vector) to detect incorrect usage of the standard libraryPutrescible
How do you compile your project? Have you add -D_LIBCPP_DEBUG=1 to CFLAG?Meistersinger
yes, I add -D_LIBCPP_DEBUG=1 to cxxflags ,then debug functions in std headers are enabled, but no implementation in released version of std lib.Putrescible
I
3

I think I got this to work!

So the steps I took,

  1. Check your toolchains __config file for _LIBCPP_VERSION's value.
  2. Browse the source repo for the commit where that version was set
  3. From that commit, find the debug.cpp file
  4. Include/compile/link the debug.cpp with your project (and remember to set _LIBCPP_DEBUG=1).

For me the intermediate results for the steps were (with Catalina, XCode Version 11.3.1)

Innes answered 19/2, 2020 at 13:28 Comment(1)
Nice, worked fine! Here's a tip, just use the blame view of the __config file github.com/llvm/llvm-project/blame/main/libcxx/include/__config and click on the button to go back to the previous commit for that line, that'll allow you to quickly jump to the commit that changed it.Ilke
A
1

I try to reactivate this thread without an answer with more details.

I have the same issue. This is very easy to reproduce:

#include <string>

int main()
{
  std::string name;
  return 0;
}

and then compile with:

clang++ -D_LIBCPP_DEBUG=1 main.cpp

We obtain:

Undefined symbols for architecture x86_64:
  "std::__1::__libcpp_db::__insert_c(void*)", referenced from:
      void std::__1::__libcpp_db::__insert_c<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) in main-7ff3c5.o
  "std::__1::__libcpp_db::__erase_c(void*)", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string() in main-7ff3c5.o
  "std::__1::__c_node::~__c_node()", referenced from:
      std::__1::_C_node<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::~_C_node() in main-7ff3c5.o
  "std::__1::__get_db()", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string() in main-7ff3c5.o
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string() in main-7ff3c5.o
  "typeinfo for std::__1::__c_node", referenced from:
      typeinfo for std::__1::_C_node<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > in main-7ff3c5.o
  "vtable for std::__1::__c_node", referenced from:
      std::__1::__c_node::__c_node(void*, std::__1::__c_node*) in main-7ff3c5.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

And if I use g++-9 providen by brew (a real g++ not a disguised Apple Clang compiler), it compiles and runs perfectly.

Forgetting to use debug mode of libc++, is it the only solution?

NB: I saw the same question in https://forums.developer.apple.com/thread/99194 and still no answer.

Arrange answered 1/12, 2019 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.