【问题标题】:c++ segfault before main gtest主要gtest之前的c ++ segfault
【发布时间】:2016-11-29 18:39:00
【问题描述】:

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

#include <gtest/gtest.h>

using namespace std;

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

堆栈跟踪:

程序收到信号SIGSEGV,分段错误。 0x0000000000000000 在 ?? ()

#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 ()

这是 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 

更新 0: 我在 Ubuntu 16.04 上遇到了这个问题。我用 14.04 创建了一个 VM,复制了代码,一切正常。没有段错误。所以 16.04 的某些不同之处似乎是造成这种情况的原因。

更新 1: 我开始认为这是由 googletest 引起的。我不知道为什么它适用于 14.04 而不是 16.06。任何包含和使用 google test 的东西都将因为立即出现段错误而无法运行。

【问题讨论】:

标签: c++ googletest


【解决方案1】:

您为什么使用 gcc ?最好使用g++。上面的代码使用g++编译时不会产生错误,运行时会打印argc的值为1,argv是某个随机地址,因为没有提供命令行参数。

__static_initialization_and_destruction_0 的出现是因为 gccgcc 为每个需要调用静态构造函数的翻译单元创建一个 __static_initialization_and_destruction_0。然后它将__do_global_ctors_aux 放入.ctors 部分,然后在每个翻译单元上调用__static_initialization_and_destruction_0

【讨论】:

  • 对不起,这是使用 g++,我打错了。我正在添加运行到我的问题的行。
【解决方案2】:

就我而言,我使用 Cmake + Conan 并静态链接每个库。
我已将 pthreads 作为 Google 测试的最后一个依赖项。
我已应用 here 的解决方案将 pthreads--whole-archive 标志链接起来。

Cmake 示例(参见docs):

target_link_libraries(my_app gtest_main gtest -Wl,--whole-archive -lpthread -Wl,--no-whole-archive)

之后就成功了。

【讨论】:

    猜你喜欢
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 2016-07-22
    • 2020-08-20
    • 1970-01-01
    相关资源
    最近更新 更多