b.b*_*old 9 c++ compilation g++ googletest 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();
}
Run Code Online (Sandbox Code Playgroud)
问题:
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...>
Run Code Online (Sandbox Code Playgroud)
所以sparsehash包括/usr/include/c++/5/tr1/tuplegtest包含/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: }
...
Run Code Online (Sandbox Code Playgroud)
我能理解为什么这会导致问题,但到目前为止我不明白为什么这只会在我的设置中出现.我定期安装google-sparsehash(git clone然后./configure && make && sudo make install),我的项目是一个CMake项目,它有一个googletest的git子模式,并将其构建为依赖项/子文件夹.此设置适用于所有未(间接)包含sparsehash标头的测试.
编辑:所以如果我添加-DGTEST_HAS_TR1_TUPLE=0 -DGTEST_USE_OWN_TR1_TUPLE=0为编译器标志似乎编译.我不确定为什么这是必要的,如果这是正确的做法
由于环境而改变 gtest 的完整定义集显示在gtest-port.h文件中。该文档还为您提供了TR1 元组配置的详细信息。
取决于您的环境。你可能正在做正确的事。GoogleTest 应该为你做检测,在实践中我很少看到它工作,我通常定义与你相同的标志。