【问题标题】:How to use (link) debug version of libc++ in macOS?如何在 macOS 中使用(链接)调试版本的 libc++?
【发布时间】:2018-03-27 08:37:13
【问题描述】:

我想在macOS中启用libc++的调试版本,所以我在cxx_build_flags[Debug Version of libC++]中定义了_LIBCPP_DEBUG=1, 但无法链接libc++的调试功能。我猜我的系统中只有libc++ 的发布版本,那么如何在macOS 中获得libc++ 的调试版本

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> >&)

【问题讨论】:

  • 为什么要使用标准库的调试版本?您要解决的实际问题是什么?
  • 我只是想开启断言功能(例如向量中的索引检查)来检测标准库的错误使用
  • 你如何编译你的项目?你有把-D_LIBCPP_DEBUG=1加到CFLAG吗?
  • 是的,我将 -D_LIBCPP_DEBUG=1 添加到 cxxflags ,然后启用 std 头中的调试功能,但在 std lib 的发布版本中没有实现。

标签: c++ macos clang llvm libc++


【解决方案1】:

我尝试重新激活此线程,但没有提供更多详细信息。

我也有同样的问题。这很容易重现:

#include <string>

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

然后编译:

clang++ -D_LIBCPP_DEBUG=1 main.cpp

我们得到:

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)

如果我使用 brew 提供的 g++-9(一个真正的 g++ 而不是伪装的 Apple Clang 编译器),它可以完美地编译和运行。

忘记使用libc++的debug模式,只能这样解决吗?

注意:我在https://forums.developer.apple.com/thread/99194 看到了同样的问题 仍然没有答案。

【讨论】:

    【解决方案2】:

    我想我得到了这个工作!

    所以我采取的步骤,

    1. 检查您的工具链 __config 文件以获取 _LIBCPP_VERSION 的值。
    2. 浏览设置该版本的提交的源代码库
    3. 从该提交中,找到 debug.cpp 文件
    4. 在您的项目中包含/编译/链接debug.cpp(并记住设置_LIBCPP_DEBUG=1)。

    对我来说,这些步骤的中间结果是(使用 Catalina,XCode 版本 11.3.1)

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-17
    • 2021-06-10
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多