【问题标题】:No source available for "libstdc++-6..."“libstdc++-6...”没有可用的源代码
【发布时间】:2014-11-16 20:00:20
【问题描述】:

我无法运行使用 MinGW-w64 和 Eclipse 作为 IDE 构建的程序。一旦我运行它,控制台中没有任何输出,来自 Windows appers 的消息“myprog.exe 已停止工作”。代码很好,但是当我排除构建特定文件时它可以工作。

我个人的猜测是和代码无关(因为我在另一个项目中测试过),是代码文件的组合导致了这个问题。 在 Eclipse 的调试透视图中告诉我

没有可用于“libstdc++-6!_ZSt9use_facetISt9money_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEEERKT_RKSt6locale() at 0x6fcb9c0a”的源代码

有人建议在链接器选项中添加这些标志:

-static-libstdc++ -static-libgcc

它不起作用,但错误消息不同:

没有可用于“std::local_Rb_tree_decrement() at 0x47379a”的源

主要是一个简单的Hello World打印。

命令:

21:01:55 **** Rebuild of configuration Debug for project autoMath ****
Info: Internal Builder is used for build
g++ -std=c++1y -O0 -g3 -Wall -c -fmessage-length=0 -o core\Operator.o ..\core\Operator.cpp 
g++ -std=c++1y -O0 -g3 -Wall -c -fmessage-length=0 -o autoMath.o ..\autoMath.cpp 
g++ -std=c++1y -O0 -g3 -Wall -c -fmessage-length=0 -o core\operand\Integer.o ..\core\operand\Integer.cpp 
g++ -std=c++1y -O0 -g3 -Wall -c -fmessage-length=0 -o core\operand\Variable.o ..\core\operand\Variable.cpp 
g++ -std=c++1y -O0 -g3 -Wall -c -fmessage-length=0 -o core\operand\Constant.o ..\core\operand\Constant.cpp 
g++ -std=c++1y -O0 -g3 -Wall -c -fmessage-length=0 -o core\operand\Vector.o ..\core\operand\Vector.cpp 
g++ -std=c++1y -O0 -g3 -Wall -c -fmessage-length=0 -o core\operand\OperandCore.o ..\core\operand\OperandCore.cpp 
g++ -std=c++1y -O0 -g3 -Wall -c -fmessage-length=0 -o core\Op.o ..\core\Op.cpp 
g++ -std=c++1y -O0 -g3 -Wall -c -fmessage-length=0 -o core\operator\Unary.o ..\core\operator\Unary.cpp 
g++ -std=c++1y -O0 -g3 -Wall -c -fmessage-length=0 -o core\operator\OperatorCore.o ..\core\operator\OperatorCore.cpp 
g++ -std=c++1y -O0 -g3 -Wall -c -fmessage-length=0 -o core\operator\Binary.o ..\core\operator\Binary.cpp 
g++ -std=c++1y -O0 -g3 -Wall -c -fmessage-length=0 -o core\Operand.o ..\core\Operand.cpp 
g++ -static-libstdc++ -static-libgcc -o autoMath core\operator\Unary.o core\operator\OperatorCore.o core\operator\Binary.o core\operand\Vector.o core\operand\Variable.o core\operand\OperandCore.o core\operand\Integer.o core\operand\Constant.o core\Operator.o core\Operand.o core\Op.o autoMath.o

编辑:导致执行失败的文件的源代码

//Binary.hpp
#include "OperatorCore.hpp"
#include "../../utility/StaticPool.hpp"
class Binary final : public OperatorCore, public StaticPool<Binary> {
    friend class StaticPool<Binary>;
public:
    enum IDs {SUM = 0, SUB, MUL, DIV, POW};
protected:
    Binary(IDs ID, std::string name, int precedence, bool revOrder = false);
public:
    const bool isAssociative;
    const bool revOrder;
    ~Binary();
    std::string print(bool latex = false) const;
    operator int() const;
};
//Binary.cpp
#include "Binary.hpp"
template<>
const Binary StaticPool<Binary>::pool[] = {
        {Binary::SUM, "+", 1}//deleting this line fix the problem
};
Binary::Binary(IDs ID, std::string name, int precedence, bool revOrder) :
        OperatorCore(name, precedence, name), StaticPool<Binary>(ID), isAssociative(true), revOrder(revOrder) {}
Binary::~Binary() {}
std::string Binary::print(bool latex) const {return name;}
Binary::operator int() const {return ID;}

“一元”类完全相同,但不会导致程序失败

【问题讨论】:

  • 我的猜测是您的代码有问题。有时,您的程序可以使用一个工具链,但在另一个工具链中失败,但它一开始只是偶然的。您的堆可能已损坏。
  • 有关缺少源的消息应该来自调试器,并且仅在程序首先崩溃时才会出现。
  • 如何验证执行是否到达 main(我是 C/C++ 新手)?
  • 看起来崩溃发生在main() 之前,此时正在调用全局构造函数。我这样说是因为__do_global_ctors() 在堆栈跟踪中。
  • 但这也是“__do_global_ctors() at 0x405245”的无源代码

标签: c++ gcc dll linker mingw-w64


【解决方案1】:

您可以忽略“无可用来源”消息。您收到此消息是因为您正在使用调试器查看在标准库中执行函数的堆栈帧,并且标准库的源代码不可用。源不可用也没关系,反正这个bug可能不在标准库中。

在您的工具链中遇到真正的错误非常罕见

如何读取堆栈跟踪

我创建了一个在我的系统上崩溃的小程序。

#include <vector>
#include <algorithm>

int main() {
    std::vector<int> v { 9, 8, 7, 6, 5, 4, 3 };
    std::sort(std::begin(v), std::begin(v) + 16);
    return 0;
}

当我在 gdb 中运行它时,我得到以下堆栈跟踪:

#0 0x00007ffff7246107 在 __GI_raise (sig=sig@entry=6) 在../nptl/sysdeps/unix/sysv/linux/raise.c:56 #1 0x00007ffff72474e8 in __GI_abort () at abort.c:89 #2 0x00007ffff7284044 in __libc_message (do_abort=do_abort@entry=1, fmt=fmt@entry=0x7ffff7376c60 "*** `%s' 中的错误: %s: 0x%s *** ") 在../sysdeps/posix/libc_fatal.c:175 #3 0x00007ffff728981e 在 malloc_printerr (action=1, str=0x7ffff7376e20 "free(): 下一个尺寸无效(快速)", ptr=) 在 malloc.c:4996 #4 0x00007ffff728a526 in _int_free (av=, p=, have_lock=0) 在 malloc.c:3840 #5 0x0000000000401456 in __gnu_cxx::new_allocator::deallocate(int*, unsigned long) () #6 0x00000000004010ca in std::allocator_traits<:allocator> >::deallocate(std::allocator&, int*, unsigned long) () #7 0x0000000000400dba 在 std::_Vector_base >::_M_deallocate(int*, unsigned long) () #8 0x0000000000400b95 在 std::_Vector_base >::~_Vector_base() () #9 0x0000000000400a6b 在 std::vector >::~vector() () #10 0x0000000000400878 in main ()

您可以看到第 10 帧在我的代码中,但第 0-9 帧在标准库中。这是比较正常的。您还会注意到在错误代码执行完毕后发生了崩溃:对std::sort() 的调用是导致崩溃的原因,但直到main() 返回时才会发生崩溃。

调试崩溃可能非常棘手。

查找错误的工具

您正在使用 MinGW,即 GCC,这意味着您可能可以访问地址清理程序。尝试将 -fsanitize=address 添加到您的编译和链接标志以进行调试构建。

当我使用-fsanitize=address 编译我的测试程序时,程序在std::sort() 中崩溃,这更接近main() 中的实际错误。地址清理器将使您的程序更快地崩溃,因此更容易调试。

类似的工具是 Valgrind。

哪里出错了?

我只能猜测,因为我看不到您的任何源代码。但看起来Binary 的构造函数中存在错误,或者Binary 类型的全局变量的构造方式错误。请记住,全局变量可以按任何顺序初始化,因此如果您的代码使用任何尚未初始化的全局变量,您的代码可能会崩溃。这是 C++ 程序中极其常见的错误来源。

【讨论】:

  • 有了那个标志我得到C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lasancollect2.exe: error: ld returned 1 exit status
  • @biowep:您的系统上似乎没有安装地址清理程序库,但如果安装了 GCC 支持使用它。
  • 我发现了问题,是在基类的构造函数中。带有矢量的示例启发了很多。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-08
  • 1970-01-01
  • 2014-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-20
相关资源
最近更新 更多