主gtest之前的c ++ segfault

Eri*_*ton 4 c++ googletest

我在main之前得到了一个堆栈跟踪:

#include <gtest/gtest.h>

using namespace std;

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

程序收到信号SIGSEGV,分段故障。0x0000000000000000 in ?? ()

#0  0x0000000000000000 in ?? ()
#1  0x00000000004e0b51 in std::locale::_S_initialize() ()
#2  0x00000000004e0b93 in std::locale::locale() ()
#3  0x000000000050d524 in std::ios_base::Init::Init() ()
#4  0x0000000000401581 in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) at /usr/include/c++/4.9/iostream:74
#5  0x00000000004015b3 in _GLOBAL__sub_I_testsmain.cpp(void) () at ../../../bdf_cpp_tests/testsmain.cpp:18
#6  0x000000000053cdd7 in __libc_csu_init ()
#7  0x000000000053c3de in generic_start_main ()
#8  0x000000000053c62a in __libc_start_main ()
#9  0x00000000004013f9 in _start ()
Run Code Online (Sandbox Code Playgroud)

这是qmake 5.7和g ++ 4.9.4

我相信这是运行的命令:

g++ -c -m64 -pipe -std=gnu++11 -std=c++11 -Werror -pedantic -DTEST_RESOURCE_DIR=\"/home/eric/Documents/BDFCppLib/test_resources\" -DTEST_OUTPUT_DIR=\"/home/eric/Documents/BDFCppLib/test_resources/output\" -g -std=gnu++11 -Wall -W -fPIC -DDEBUG -I../../../bdf_cpp_tests -I/home/eric/Documents/BDFCppLib/shadow-ant/ubuntu64_gcc49_dev/bdf_cpp_tests -I../../../bdf_cpp_sdk/include -I../../../lib/ubuntu64_gcc49_dev/unpack/chunk_cpp/include -I../../../lib/ubuntu64_gcc49_dev/unpack/system/include -I/home/eric/Software/qt/5.7/gcc_64/mkspecs/linux-g++-64 -o testsmain.o ../../../bdf_cpp_tests/testsmain.cpp

g++  -o ../../../build/ubuntu64_gcc49_dev/bin/bdf_cpp_run_tests testsmain.o testutils.o   -pthread -lrt -L/home/eric/Documents/BDFCppLib/build/ubuntu64_gcc49_dev/lib -static -lbdf -L/home/eric/Documents/BDFCppLib/lib/ubuntu64_gcc49_dev/unpack/chunk_cpp/lib -static -lchunk -L/home/eric/Documents/BDFCppLib/lib/ubuntu64_gcc49_dev/unpack/system/lib -lgtest 
Run Code Online (Sandbox Code Playgroud)

更新0:我正在Ubuntu 16.04上遇到此问题。我用14.04创建了一个VM,复制了代码,一切正常。没有段错误。所以16.04似乎有所不同。

更新1:我开始认为这是由googletest引起的。我不知道为什么它将在14.04而不是16.06下运行。由于立即出现段错误,所有包含和使用Google测试的内容都将无法运行。

Jar*_*vis 5

你为什么用gcc?最好使用g++。上面的代码使用编译时g++,不会产生任何错误;运行时,由于没有提供命令行参数,因此打印的值将为argc1,并且argv将为随机地址。

__static_initialization_and_destruction_0发生的原因是gcc。为每个需要调用静态构造函数的翻译单元gcc创建一个__static_initialization_and_destruction_0。然后将其__do_global_ctors_aux放入该.ctors部分,然后调用__static_initialization_and_destruction_0每个翻译单元。