簡體   English   中英

主gtest之前的c ++ segfault

[英]c++ segfault before main gtest

我在main之前得到了一個堆棧跟蹤:

#include <gtest/gtest.h>

using namespace std;

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

堆棧跟蹤:

程序收到信號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 ()

這是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測試的內容都將無法運行。

為什么要使用gcc 最好使用g++ 上面的代碼在使用g++編譯時不會產生錯誤,並且在運行時將argc的值打印為1,並將argv的值打印為某個隨機地址,因為沒有提供命令行參數。

由於gcc發生__static_initialization_and_destruction_0 gcc為每個需要調用靜態構造函數的翻譯單元創建一個__static_initialization_and_destruction_0 然后將__do_global_ctors_aux放入.ctors節,然后在每個翻譯單元上調用__static_initialization_and_destruction_0

就我而言,我使用的是Cmake + Conan,並靜態鏈接了每個庫。
我將pthreads作為Google Test的最后一個依賴項。
我從這里開始應用解決方案,以--whole-archive標志鏈接pthreads

Cmake示例(請參閱docs ):

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

之后,它起作用了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM