<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: }
...
我可以理解为什么这会导致问题,但到目前为止我不明白为什么这似乎只发生在我的设置中。git clone
我定期(然后)安装了 google-sparsehash ./configure && make && sudo make install
,我的项目是一个 CMake 项目,它有一个用于 googletest 的 git 子模式并将其构建为依赖项/子文件夹。此设置适用于所有不(间接)包含 sparsehash 标头的测试。
-DGTEST_HAS_TR1_TUPLE=0 -DGTEST_USE_OWN_TR1_TUPLE=0
编辑:所以如果我添加为编译器标志,它似乎可以编译。我不知道为什么这是必要的,如果这是正确的做法