Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么与pthread链接会导致分段错误?_C++_Linux_Gcc_Segmentation Fault_Pthreads - Fatal编程技术网

C++ 为什么与pthread链接会导致分段错误?

C++ 为什么与pthread链接会导致分段错误?,c++,linux,gcc,segmentation-fault,pthreads,C++,Linux,Gcc,Segmentation Fault,Pthreads,我有一个精简的简单程序,带有一个静态变量('abc.cpp'): 但是,如果我在pthread库中链接 > g++ -ggdb -lpthread abc.cpp -o abc > ./abc Segmentation fault (core dumped) > gdb abc (gdb) run Starting program: abc Program received signal SIGSEGV, Segmentation fault. 0x000000000000

我有一个精简的简单程序,带有一个静态变量('abc.cpp'):

但是,如果我在pthread库中链接

> g++ -ggdb -lpthread abc.cpp -o abc
> ./abc
Segmentation fault (core dumped)

> gdb abc
(gdb) run
Starting program: abc

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) where
#0  0x0000000000000000 in ?? ()
#1  0x00007ffff7b01681 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#2  0x00007ffff7b016c3 in std::locale::locale() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007ffff7afe244 in std::ios_base::Init::Init() ()
   from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x0000000000400d03 in __static_initialization_and_destruction_0 (__initialize_p=1,
    __priority=65535) at /usr/include/c++/4.9/iostream:74
#5  0x0000000000400d2c in _GLOBAL__sub_I_main () at abc.cpp:8
#6  0x0000000000400d7d in __libc_csu_init ()
#7  0x00007ffff74a6e55 in __libc_start_main (main=0x400c06 <main(int, char**)>, argc=1,
    argv=0x7fffffffdb58, init=0x400d30 <__libc_csu_init>, fini=<optimised out>,
    rtld_fini=<optimised out>, stack_end=0x7fffffffdb48) at libc-start.c:246
#8  0x0000000000400b39 in _start ()
这为什么会导致segfault?

旁注:叮当声有效

> clang++ -ggdb -lpthread abc.cpp -o abc
> ./abc
Hello world
版本信息:

> g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.9/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.9.2-0ubuntu1~14.04' --with-bugurl=file:///usr/share/doc/gcc-4.9/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.9 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.9 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.9-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.9.2 (Ubuntu 4.9.2-0ubuntu1~14.04)

> dpkg -l 'libstdc++6*'
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                Version        Architecture   Description
+++-===================-==============-==============-===========================================
ii  libstdc++6:amd64    5-20150329-1ub amd64          GNU Standard C++ Library v3
un  libstdc++6-4.0-dbg  <none>         <none>         (no description available)
un  libstdc++6-4.1-dbg  <none>         <none>         (no description available)
un  libstdc++6-4.2-dbg  <none>         <none>         (no description available)
un  libstdc++6-4.3-dbg  <none>         <none>         (no description available)
un  libstdc++6-4.4-dbg  <none>         <none>         (no description available)
un  libstdc++6-4.5-dbg  <none>         <none>         (no description available)
un  libstdc++6-4.6-dbg  <none>         <none>         (no description available)
un  libstdc++6-4.7-dbg  <none>         <none>         (no description available)
un  libstdc++6-4.8-dbg  <none>         <none>         (no description available)
ii  libstdc++6-4.9-dbg: 4.9.2-0ubuntu1 amd64          GNU Standard C++ Library v3 (debugging file
un  libstdc++6-5-dbg    <none>         <none>         (no description available)
un  libstdc++6-dbg      <none>         <none>         (no description available)
以及用于clang build的
ldd abc
(注意此处的pthread,而不是gcc中的pthread):

值得一提的是,我的地区:

> locale
LANG=en_AU.UTF-8
LANGUAGE=en_AU:en
LC_CTYPE="en_AU.UTF-8"
LC_NUMERIC="en_AU.UTF-8"
LC_TIME="en_AU.UTF-8"
LC_COLLATE="en_AU.UTF-8"
LC_MONETARY="en_AU.UTF-8"
LC_MESSAGES="en_AU.UTF-8"
LC_PAPER="en_AU.UTF-8"
LC_NAME="en_AU.UTF-8"
LC_ADDRESS="en_AU.UTF-8"
LC_TELEPHONE="en_AU.UTF-8"
LC_MEASUREMENT="en_AU.UTF-8"
LC_IDENTIFICATION="en_AU.UTF-8"
LC_ALL=
设置默认区域设置会产生相同的结果(以及相同的堆栈跟踪):


他是你的朋友。使用线程库不仅仅包括链接步骤中的库:文件也应该使用“-pthread”开关编译。

您安装的
libstdc++6
包甚至不在Ubuntu中

发件人:

我建议您检查
/etc/apt/sources.list
并删除导致安装程序包的行。我认为它可能是“工具链测试构建”的PPA

然后,您可以尝试使用
libstdc++6
软件包返回到一个正常的版本(适用于您安装的Ubuntu版本)。当你这样做的时候,你应该仔细检查其他包是否也受到影响。您不希望在核心库的测试版本上运行系统


顺便说一下,这也解释了为什么
gdb
没有找到调试符号,它们是用于库的另一个版本。

原来是GCC编译器或libstdc++出现了错误/损坏。与pthread链接不应导致segfault

Clang没有使用相同的libstdc++,这表明它可能是编译器。我用
gcc版本5.1.0(Ubuntu 5.1.0-0ubuntu11~14.04.1)
进行了测试,发现它工作正常,因此它确认了gcc/libstdc++错误


最初使用的GCC4.9来自一个非官方的测试编译器库()——而不是官方的Ubuntu库。然而,经过测试的GCC5.1也来自同一个存储库,因此至少修复了该缺陷。目前还不清楚官方的Ubuntu GCC 4.8是否导致了这个错误。

这个问题实际上是系统默认使用的黄金链接器中的一个bug。检查由ld--version打印的名称。 该错误似乎仍处于打开状态:


使用标准bfd链接器不会显示此问题。所使用的链接器由
gcc-fuse ld=gold
/usr/bin/ld

中的符号链接定义,所有符号都指向gcc中的一个bug,除非得到证明。您能告诉我们您实际链接的库是什么吗?@Jeff:噢!确认一下就好了。另外,确认它在5.0中已修复也会很好。@Dashwuff:我自己做的,在这里几乎没有关系。因为它看起来像是libstdc++中的空指针崩溃,所以也可以安装,那么堆栈跟踪应该会提供更多信息。这只会添加外部调试信息文件,因此不会改变您的
libstdc++
library文件。GCC文档也是您的朋友:搜索“-pthread”选项会在“RS/6000和PowerPC选项”和“Solaris 2选项”部分显示它。我使用的是x86_64,所以我觉得这个选项与我的平台无关。这不对吗?我的编译和链接是在一个命令中完成的。我确实试过了,用“-pthread”替换“-lpthread”会导致完全相同的分段错误。至少在C11中,该标志应该不再需要了。据我所知,GCC已经遵循C11内存模型十多年了。而且,正如我所怀疑的,
-pthread
编译选项对Mac x86_64上的GCC 4.9.2代码生成完全没有任何影响,这是使用汇编输出测得的。正确!我确实从中获得了GCC构建。Ubuntu14.04LTS(4.8)中包含的版本对我来说太旧了。页面,如(旧的,但同样的原则适用),似乎表明这是一个好的事情做。我认为它与实际的4.9.2版本非常接近。我更喜欢4.9或5.1,所以我甚至可能在某个时候尝试5.1…你可以从乌托邦使用
ubuntu-toolchain-r
甚至比vivid更先进,它是一个不受支持的测试包。就我个人而言,我不会在上面发展,因为你永远不会知道崩溃是你的错还是工具链的错。GCC5的特性不值得遇到这样的问题。Utopic(14.10)不是LTS,我需要使用LTS版本。啊,固体支撑和刀刃之间的张力。GCC4.9和5.1是从4.8向前迈出的一大步,所以我不愿意后退。GCC4.9是一年前发布的,所以遗憾的是它仍然没有在最前沿的“测试”版本中得到修复。非常接近官方的4.9.2版本是有意义的。确认:GCC 5.1(来自ubuntu-toolchain-r/test:
GCC 5.1.0版(ubuntu 5.1.0-0ubuntu11~14.04.1)
)确实修复了这个seg故障。那很好。因此,libstdc++的版本应该受到谴责。我将此标记为解决方案,尽管我不喜欢它的基调。请您说明seg故障是由libstdc++中的一个bug引起的,这是一个非官方的不受支持的版本,您可以对此进行改进吗?您可以引用我的GCC5.1测试来证明这个bug已经修复。然后,您可以将如何返回到官方GCC包的步骤放在后面。默认情况下,它不使用gold-我让它在使用/usr/bin/ld符号链接时使用gold。我需要金子来敲打LTO。谢谢,看起来很像,但我不能完全确定。。。是否只是编译器/链接器的组合导致了它,而不是其他具有不同编译器的组合?请记住,虽然仍然使用相同的黄金链接器,但Clang和GCC 5.1都运行良好。。。。我也(不认为?)我的使用了-Wl,--as-needed标志,就像他们在bug中所要求的那样。当ld符号链接指向默认使用的gold时--默认情况下,在标准ubuntu链接器中使用as needed,其硬编码在ubuntu附带的gcc规范中。虽然有可能存在
> clang++ -ggdb -lpthread abc.cpp -o abc
> ./abc
Hello world
> g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.9/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.9.2-0ubuntu1~14.04' --with-bugurl=file:///usr/share/doc/gcc-4.9/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.9 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.9 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.9-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.9.2 (Ubuntu 4.9.2-0ubuntu1~14.04)

> dpkg -l 'libstdc++6*'
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                Version        Architecture   Description
+++-===================-==============-==============-===========================================
ii  libstdc++6:amd64    5-20150329-1ub amd64          GNU Standard C++ Library v3
un  libstdc++6-4.0-dbg  <none>         <none>         (no description available)
un  libstdc++6-4.1-dbg  <none>         <none>         (no description available)
un  libstdc++6-4.2-dbg  <none>         <none>         (no description available)
un  libstdc++6-4.3-dbg  <none>         <none>         (no description available)
un  libstdc++6-4.4-dbg  <none>         <none>         (no description available)
un  libstdc++6-4.5-dbg  <none>         <none>         (no description available)
un  libstdc++6-4.6-dbg  <none>         <none>         (no description available)
un  libstdc++6-4.7-dbg  <none>         <none>         (no description available)
un  libstdc++6-4.8-dbg  <none>         <none>         (no description available)
ii  libstdc++6-4.9-dbg: 4.9.2-0ubuntu1 amd64          GNU Standard C++ Library v3 (debugging file
un  libstdc++6-5-dbg    <none>         <none>         (no description available)
un  libstdc++6-dbg      <none>         <none>         (no description available)
linux-vdso.so.1 => (0x00007ffef8f2f000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f87b167c000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f87b1465000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f87b109f000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f87b0d99000)
/lib64/ld-linux-x86-64.so.2 (0x00007f87b1a11000)
linux-vdso.so.1 => (0x00007fffa4cc7000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fab1f10d000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fab1ed94000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fab1ea8d000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fab1e876000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fab1e4b1000)
/lib64/ld-linux-x86-64.so.2 (0x00007fab1f347000)
> locale
LANG=en_AU.UTF-8
LANGUAGE=en_AU:en
LC_CTYPE="en_AU.UTF-8"
LC_NUMERIC="en_AU.UTF-8"
LC_TIME="en_AU.UTF-8"
LC_COLLATE="en_AU.UTF-8"
LC_MONETARY="en_AU.UTF-8"
LC_MESSAGES="en_AU.UTF-8"
LC_PAPER="en_AU.UTF-8"
LC_NAME="en_AU.UTF-8"
LC_ADDRESS="en_AU.UTF-8"
LC_TELEPHONE="en_AU.UTF-8"
LC_MEASUREMENT="en_AU.UTF-8"
LC_IDENTIFICATION="en_AU.UTF-8"
LC_ALL=
> LC_ALL=C ./abc
Segmentation fault (core dumped)
$ man gcc
ii  libstdc++6:amd64  5-20150329-1ubuntu11 amd64    GNU Standard C++ Library v3
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