1

我想用谷歌测试运行单元测试,但出现分段错误。我刚刚定义了一个全局 const 字符串和 RUN_ALL_TESTS,为什么?

gdb 结果是:

[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[==========] Running 0 tests from 0 test cases.
[==========] 0 tests from 0 test cases ran. (1 ms total)
[  PASSED  ] 0 tests.

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff6fe415c in __GI___libc_free (mem=0x603588) at malloc.c:2947
2947    malloc.c: No such file or directory.

流动的是回溯:

(gdb) bt
#0  0x00007ffff6fe415c in __GI___libc_free (mem=0x603588) at malloc.c:2947
#1  0x00007ffff75fa36f in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#2  0x00007ffff7bb25ba in void std::_Destroy<std::string>(std::string*) () from /home/caros/src/secsdk/datasecurity-upgrade/external/libgtest.so
#3  0x00007ffff7bb04bd in void std::_Destroy_aux<false>::__destroy<std::string*>(std::string*, std::string*) () from /home/caros/src/secsdk/datasecurity-upgrade/external/libgtest.so
#4  0x00007ffff7bac65c in void std::_Destroy<std::string*>(std::string*, std::string*) () from /home/caros/src/secsdk/datasecurity-upgrade/external/libgtest.so
#5  0x00007ffff7ba68f3 in void std::_Destroy<std::string*, std::string>(std::string*, std::string*, std::allocator<std::string>&) () from /home/caros/src/secsdk/datasecurity-upgrade/external/libgtest.so
#6  0x00007ffff7ba08b2 in std::vector<std::string, std::allocator<std::string> >::~vector() () from /home/caros/src/secsdk/datasecurity-upgrade/external/libgtest.so
#7  0x00007ffff6f9d53a in __cxa_finalize (d=0x7ffff7b2b440) at cxa_finalize.c:56
#8  0x00007ffff78b7ea3 in __do_global_dtors_aux () from /home/caros/src/secsdk/datasecurity-upgrade/external/libgmock.so
#9  0x00007fffffffd5e0 in ?? ()
#10 0x00007ffff7dea7da in _dl_fini () at dl-fini.c:252
Backtrace stopped: frame did not save the PC

这是我的整个源代码:

#include <iostream>                                                             
#include "gtest/gtest.h"
#include "gmock/gmock.h"
const std::string fsdfsdf = "sdfsdfsd";
int main(int argc, char* argv[]) {
    testing::InitGoogleMock(&argc, argv);
    RUN_ALL_TESTS();
    return 0;
}

我希望程序在没有分段错误的情况下完成。

4

1 回答 1

0

为什么?

您的代码似乎没有任何问题。问题可能出在环境中。

你没有说libgtest.so从哪里来libgmock.so。也许它们与您的g++. 很难说。

在 Valgrind 下运行你的程序可能会提供额外的线索。

于 2019-05-26T03:57:18.290 回答