使用gtest和google sparsehash时重新定义元组



所有以某种方式包含<gtest/gtest.h><google/dense_hash_map>的测试用例都无法为我构建。通常后者是间接包含的,但我可以像这样重现这个问题:

#include <gtest/gtest.h>
#include <google/dense_hash_map>
TEST(SparsehashTest, justPass) {
  ASSERT_TRUE(true);
};

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

问题:

In file included from /usr/include/c++/5/tr1/functional:39:0,
                 from /usr/local/include/sparsehash/dense_hash_map:106,
                 from /usr/local/include/google/dense_hash_map:34,
                 from /home/me/xxx/test/SparsehashTest.cpp:6:
/usr/include/c++/5/tr1/tuple:130:11: error: redefinition of ‘class std::tuple< <template-parameter-1-1> >’
     class tuple : public _Tuple_impl<0, _Elements...>
           ^
In file included from /home/me/xxx/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:697:0,
                 from /home/me/xxx/third_party/googletest/googletest/include/gtest/internal/gtest-internal.h:40,
                 from /home/me/xxx/third_party/googletest/googletest/include/gtest/gtest.h:58,
                 from /home/me/xxx/test/SparsehashTest.cpp:5:
/usr/include/c++/5/tuple:463:11: error: previous definition of ‘class std::tuple< <template-parameter-1-1> >’
     class tuple : public _Tuple_impl<0, _Elements...>

因此sparsehash包含/usr/include/c++/5/tr1/tuple而gtest包含/usr/include/c++/5/tuple并将其放在gtest-port.h:

中的tr1命名空间中
...
697: #include <tuple>
698: // C++11 puts its tuple into the ::std namespace rather than
699: // ::std::tr1.  gtest expects tuple to live in ::std::tr1, so put it there.
700: // This causes undefined behavior, but supported compilers react in
701: // the way we intend.
702: namespace std {
703: namespace tr1 {
704: using ::std::get;
705: using ::std::make_tuple;
706: using ::std::tuple;
707: using ::std::tuple_element;
708: using ::std::tuple_size;
709: }
...

我可以理解为什么这会导致问题,但到目前为止,我不明白为什么这似乎只发生在我的设置。我有google-sparsehash定期安装(git clone./configure && make && sudo make install),我的项目是一个CMake项目,有一个git子模式的googletest和构建它作为一个依赖/子文件夹。对于所有不(间接)包含sparsehash头的测试,此设置按预期工作。

编辑:所以如果我添加-DGTEST_HAS_TR1_TUPLE=0 -DGTEST_USE_OWN_TR1_TUPLE=0作为编译器标志,似乎可以编译。我不知道为什么这是必要的,如果它是正确的事情在这里做

我发现了这个线程,因为我有同样的问题,它提供了足够的线索,我设法通过请求用c++11编译器选项编译sparsehash来解决我的问题:

。/配置CXXFLAGS ="化= c + + 11"

由于环境而改变gtest的完整定义集显示在gtest-port.h文件中。该文档还提供了TR1元组配置的详细信息。

取决于您的环境。也许你做的是对的。GoogleTest应该为你做检测,在实践中我很少看到它工作,我通常定义相同的标志和你的

最新更新