1

在具有 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) 

下面的代码只是给出了一个段错误:

#include <iostream>

void init()__attribute__((constructor));

int main()
{
    return 0;
};
void init()
{
    std::cout<<"test";
}

预计在进入main之前构建时会打印出test 。也尝试了构造函数(65530)和相同的。我绝对无法找出问题所在。我不希望这么少行的代码崩溃。另外我必须提到,上面的代码过去可以使用很长时间(3 年)使用更旧(我不记得哪个)版本的 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 ()
4

0 回答 0