Why does linking with pthread cause a segmentation fault?

前端 未结 4 513
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 01:15

I have a stripped down simple program with a static variable (\'abc.cpp\'):

#include 

int main(int, char**)
{
  static const std::string a(\         


        
相关标签:
4条回答
  • 2021-01-14 01:21

    The libstdc++6 package you have installed is not even in Ubuntu!

    ii  libstdc++6:amd64  5-20150329-1ubuntu11 amd64    GNU Standard C++ Library v3
    

    From packages.ubuntu.com:

    trusty (14.04LTS) (libs): GNU Standard C++ Library v3
        4.8.2-19ubuntu1: amd64 i386
    utopic (libs): GNU Standard C++ Library v3
        4.9.1-16ubuntu6: amd64 i386
    vivid (libs): GNU Standard C++ Library v3
        4.9.2-10ubuntu13: amd64 i386 
    

    I would recommend checking your /etc/apt/sources.list and removing the line that caused installation of the package. I think it may be ppa:ubuntu-toolchain-r/test, a PPA for "Toolchain test builds"...

    Then you can try to downgrade your libstdc++6 package to get back to a sane version (appropriate for the Ubuntu version you have installed). While you are at it, you should carefully check if other packages are affected as well. You don't want to run your system on test-builds of your core libraries.

    By the way, that also explains why gdb did not find the debug symbols, they were for another version of the library.

    0 讨论(0)
  • 2021-01-14 01:21

    It turns out it was the GCC compiler or libstdc++ that is buggy/broken. Linking with pthread shouldn't cause a segfault.

    Clang didn't segfault, which is using the same libstdc++, so that suggests it may be the compiler. I tested with gcc version 5.1.0 (Ubuntu 5.1.0-0ubuntu11~14.04.1) and found it worked, so it confirms the GCC/libstdc++ bug.

    The original GCC 4.9 which was used was from an unofficial test compiler repository (https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test) - not the official Ubuntu repository. However the GCC 5.1 that was tested, was also from the same repository, so at least the bug was fixed there. It is not known as to whether the official Ubuntu GCC 4.8 causes this segfault or not.

    0 讨论(0)
  • 2021-01-14 01:22
    $ man gcc
    

    is your friend. There is more to using the threading library than just including the library in the linking step: the files should be compiled using the "-pthread" switch too.

    0 讨论(0)
  • 2021-01-14 01:31

    The issue is actually a bug in the gold linker your system is probably using by default. Check the name printed by ld --version. The bug appears to still be open: https://sourceware.org/bugzilla/show_bug.cgi?id=16417

    Using the standard bfd linker does not show this problem. The linker used is either defined by gcc -fuse-ld=gold or the symlink in /usr/bin/ld

    0 讨论(0)
提交回复
热议问题