C++ 在构造函数中使用cout会导致分段错误

C++ 在构造函数中使用cout会导致分段错误,c++,gcc,segmentation-fault,cout,C++,Gcc,Segmentation Fault,Cout,在使用ubuntu 13.07和gcc的系统上: Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.3-1ubuntu1' --with-b

在使用ubuntu 13.07和gcc的系统上:

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v 
--with-pkgversion='Ubuntu/Linaro 4.7.3-1ubuntu1' 
--with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs 
--enable-languages=c,c++,go,fortran,objc,obj-c++ 
--prefix=/usr --program-suffix=-4.7 --enable-shared 
--enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext 
--enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 
--libdir=/usr/lib --enable-nls --with-sysroot=/ 
--enable-clocale=gnu 
--enable-libstdcxx-debug --enable-libstdcxx-time=yes 
--enable-gnu-unique-object --enable-plugin --with-system-zlib 
--enable-objc-gc --with-cloog --enable-cloog-backend=ppl 
--disable-cloog-version-check --disable-ppl-version-check 
--enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 
--with-multilib-list=m32,m64,mx32 --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.7.3 (Ubuntu/Linaro 4.7.3-1ubuntu1) 
以下代码仅给出seg故障:

#include <iostream>

void init()__attribute__((constructor));

int main()
{
    return 0;
};
void init()
{
    std::cout<<"test";
}
在进入主管道之前,预计在施工时打印测试。还尝试了constructor65530和同样的方法。 我绝对找不出哪里出了问题。我不希望只有这么少行的代码崩溃。另外,我必须提到的是,上面的代码在3年前使用了很长一段时间,使用了非常旧的ERI,但我不记得是哪个版本的GCC。我是否犯了一个非常明显的错误

以下是gdb跟踪,供您参考

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b6c131 in std::ostream::sentry::sentry(std::ostream&) ()
   from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
(gdb)
(gdb) bt
#0  0x00007ffff7b6c131 in std::ostream::sentry::sentry(std::ostream&) ()
   from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x00007ffff7b6c829 in std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char,
   from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#2  0x00007ffff7b6cc0f in std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_t
#3  0x000000000040079a in init () at test.cpp:12
#4  0x000000000040084d in __libc_csu_init ()
#5  0x00007ffff7730e35 in __libc_start_main (main=0x40077c <main()>, argc=1,
    ubp_av=0x7fffffffd3f8, init=0x4007f0 <__libc_csu_init>,
    fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffd3e8)
    at libc-start.c:219
#6  0x00000000004006b9 in _start ()

看起来像是静态初始化顺序的典型问题,即当您的ctor运行时,还没有构造cout。可能有兴趣。@JerryCoffin,但cout必须先建造。Avind,你能用一个实际的构造函数而不是GCC特有的功能重现这个问题吗?@Potatoswatter:它需要在普通构造函数之前构造,是的。我很确定标准对使用gcc的构造函数属性的代码没有任何要求。