Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ segfault before main gtest

Tags:

c++

googletest

I'm getting a stacktrace before main:

#include <gtest/gtest.h>

using namespace std;

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

stacktrace:

Program received signal SIGSEGV, Segmentation fault. 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 ()

This is qmake 5.7 and g++ 4.9.4

I believe this is the command that runs:

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 

UPDATE 0: I'm running into this issue on Ubuntu 16.04. I created a VM with 14.04, copied the code over and everything worked. No segfault. So something is different about 16.04 that seems to be causing this.

UPDATE 1: I'm starting to think this is being caused by googletest. I don't know why it would work with 14.04 and not 16.06. Anything that includes and uses google test will not be able to run because of an immediate segfault.

like image 251
Eric Fulton Avatar asked Nov 29 '16 18:11

Eric Fulton


2 Answers

Why are you using gcc ? It's better to use g++. The above code when compiled using g++, generates no error, and when run, prints the value of argc to be 1, and argv to be some random address, since no command line argument is provided.

__static_initialization_and_destruction_0 occurs because of gcc. gcc creates a __static_initialization_and_destruction_0 for every translation unit that needs static constructors to be called. Then it places __do_global_ctors_aux into the .ctors section, which then calls __static_initialization_and_destruction_0 on each translation unit.

like image 132
Jarvis Avatar answered Sep 30 '22 00:09

Jarvis


In my case, I was using Cmake + Conan and linked every library statically.
I've had pthreads as a last dependency for Google Test.
I've applied solution from here to link pthreads with --whole-archive flag.

Example for Cmake (see docs):

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

After that it worked.

like image 34
Vadim Kotov Avatar answered Sep 30 '22 01:09

Vadim Kotov