【问题标题】:C++ basic program segmentation faultC++基本程序分段错误
【发布时间】:2014-12-18 17:17:36
【问题描述】:

这是我的 CPP 计划

#include <iostream>

int main(int argc, char *argv[]){
     return 0;
}

编译和执行这个,会导致段错误:

[18:08 ~] > g++ test.cpp
[18:09 ~] > ./a.out
Segmentation fault

请问有人知道这个问题吗? 非常感谢您的帮助。

以下是一些可能有用的细节:

[18:13 ~] > ldd a.out
 linux-vdso.so.1 =>  (0x00007fff3fdff000)
 libstdc++.so.6 => /usr/lib/gcc/x86_64-redhat-linux/3.4.6/libstdc++.so.6 (0x00007fc8f4a53000)
 libm.so.6 => /lib64/libm.so.6 (0x00007fc8f47ce000)
 libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fc8f45b8000)
 libc.so.6 => /lib64/libc.so.6 (0x00007fc8f4225000)
 /lib64/ld-linux-x86-64.so.2 (0x00007fc8f4ca5000)

还有gdb:

[18:15 ~] > gdb ./a.out
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from ...done.
(gdb) run
Starting program: a.out

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7bd48ec in std::ios_base::Init::Init() () from /usr/lib/gcc/x86_64-redhat-linux/3.4.6/libstdc++.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.107.el6.x86_64 libgcc-4.4.7-3.el6.x86_64
(gdb) bt
#0  0x00007ffff7bd48ec in std::ios_base::Init::Init() () from /usr/lib/gcc/x86_64-redhat-linux/3.4.6/libstdc++.so.6
#1  0x0000000000400831 in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) at /usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/iostream:77
#2  0x0000000000400873 in global constructors keyed to main() () at test.cpp:7
#3  0x0000000000400976 in __do_global_ctors_aux ()
#4  0x00000000004005a3 in _init ()
#5  0x00007fffffffe448 in ?? ()
#6  0x0000000000400905 in __libc_csu_init ()
#7  0x00007ffff7385c70 in __libc_start_main () from /lib64/libc.so.6
#8  0x0000000000400659 in _start ()
(gdb)

g++

[10:14 ~] > g++ -v
Reading specs from /usr/lib/gcc/x86_64-redhat-linux/3.4.6/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-languages=c,c++,f77 --disable-libgcj --host=x86_64-redhat-linux
Thread model: posix
gcc version 3.4.6 20060404 (Red Hat 3.4.6-19.el6)

最后一件事

[10:25 ~] > ls /usr/lib/gcc/x86_64-redhat-linux
3.4.6

谢谢。

【问题讨论】:

  • g++ 报告什么版本? (g++ -v)
  • 你好,马特,谢谢。我用更多细节更新了我的第一个线程

标签: c++ segmentation-fault g++


【解决方案1】:

很明显,您的问题是您的 g++ 安装已损坏。我从消息中看到您拥有 RHEL6,但不知何故拥有 3.4.6 版的 libstdc++。

我的 RHEL6 系统有 4.4.6 版本的 gcc、libstdc++ 等。我假设这个旧版本的 libstdc++ 与您的问题有关。

您安装了哪些版本的 gcc、g++ 和 libstdc++?从其他消息来看,您似乎有 4.4.7,但您是否恰好有多个版本或混合版本?

rpm -q gcc g++ libstdc++

另外,/usr/lib/gcc/x86_64-redhat-linux 下还有哪些目录?

ls /usr/lib/gcc/x86_64-redhat-linux

【讨论】:

  • 你好罗素,谢谢。我用更多细节更新了我的第一个线程
  • 此时我最好的猜测是 g++ 和 libgcc 的版本不匹配,并且它们不兼容。从您迄今为止提供的信息来看,您似乎拥有 g++ 和 libstdc++ 的 3.4.6 版本,但 libgcc 的版本为 4.4.7。我不知道你有什么版本的 gcc,但我怀疑是 4.4.7。我建议升级 g++ 和 libstdc++,使它们与您拥有的 gcc 版本相匹配。除非 gcc 也是 3.4.6,在这种情况下你可能只是降级 libgcc 以匹配。
  • 如果使版本完全匹配并不能解决问题,我建议您前往 gcc 邮件列表寻求帮助。
【解决方案2】:

我认为你在安装 g++ 时遗漏了一些东西

Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.107.el6.x86_64 libgcc-4.4.7-3.el6.x86_64

【讨论】:

    猜你喜欢
    • 2016-03-31
    • 2021-01-08
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 2018-11-18
    • 2011-05-23
    相关资源
    最近更新 更多