【问题标题】:Compiling with Clang using Libc++ undefined references使用 Libc++ 未定义引用使用 Clang 进行编译
【发布时间】:2011-10-24 09:48:51
【问题描述】:

第一对太长了,无法参考。当我尝试使用来自 SVN 的 clang 和 libc++ 编译 clang++ -stdlib=libc++ ../main.cc ... 时出现此错误。

error: undefined reference to 'typeinfo for char const*'
error: undefined reference to '__cxa_allocate_exception'
error: undefined reference to '__cxa_throw'
/tmp/cc-pbn00y.o:../main.cc:function std::__1::deque<double, std::__1::allocator<double> >::__add_back_capacity(): error: undefined reference to '__cxa_begin_catch'
/tmp/cc-pbn00y.o:../main.cc:function std::__1::deque<double, std::__1::allocator<double> >::__add_back_capacity(): error: undefined reference to '__cxa_rethrow'
/tmp/cc-pbn00y.o:../main.cc:function std::__1::deque<double, std::__1::allocator<double> >::__add_back_capacity(): error: undefined reference to '__cxa_end_catch'
/tmp/cc-pbn00y.o(.eh_frame+0xbd3): error: undefined reference to '__gxx_personality_v0'

解决方案:感谢其中一个答案,我知道解决方案。 libc++ 不能像 libstdc++ 一样单独使用,它必须与 libc++abi 一起链接。不过libc++abi还没有完成,所以目前使用libc++似乎有点不完整,但完成后还是我的首选。

2012 年 5 月 26 日更新: libc++abi 现在已完成 C++,我一直在成功使用 clang++,如下所示 clang++ -std=c++11 -stdlib=libc++ -lc++abi

【问题讨论】:

标签: c++ clang std undefined-reference libc++


【解决方案1】:

我相信 libc++ 还不支持所有异常函数。查看状态页面:

http://libcxxabi.llvm.org/spec.html

你可能会链接到 gnu 的 libstdc++

【讨论】:

  • 好吧,打我一巴掌,叫我南希,看起来像。我可以同时链接它们并覆盖范围吗?
  • 这可能是可能的,如果您非常小心地以正确的顺序为链接器指定库,并确保您了解链接器如何处理对象的顺序以及它如何解析符号可能在多个地方定义。
  • 好的,所以我下载了 libc++abi 并将其链接,除typeinfo for char const*__gxx_personality_v0 之外的所有错误都消失了。
  • 原谅我的无知,但是你如何指定这个?
  • 没关系,这似乎是-stdlib=libc++,但对我有用的是-lstdc++.6
【解决方案2】:

以下是适用于 clang 和 libc++ 的 Ubuntu Vivid 软件包对我有用的方法:

clang++ -std=c++11 -stdlib=libc++ <object files> -lc++abi -lsupc++

目标文件位于-l 标志之前很重要,例如当您使用异常时。显然,如果您使用针对 libstdc++ 编译的库并在这些接口中使用任何 STL 类型,这仍然不会链接。

【讨论】:

    【解决方案3】:

    这似乎您正在使用异常处理,但编译器中未启用它。尝试将 -fexceptions 传递给命令行。

    【讨论】:

      【解决方案4】:

      我只是添加这个答案,因为我刚才确实犯了这个错误。它编译了我几天来写的大部分内容,但现在它开始抛出未定义的引用错误......

      所以...我...可能是用clang而不是clang++进行编译。是的。这就是错误的全部。 clang++ 包括 C++ 库的东西,clang 没有。糟糕!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-27
        • 1970-01-01
        • 2011-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-02
        • 2012-01-19
        相关资源
        最近更新 更多