使用gtest和google sparsehash时重新定义元组
Redefinition of tuple when using gtest and 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:
...
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应该为你做检测,在实践中我很少看到它工作,我通常定义相同的标志和你的
- C++:TypeDef使用元组
- Pybind11:将元组列表从Python传递到C++
- std::ranges::elements_view,用于自定义类似元组的数据
- 如何从参数包定义值类型的元组
- 元组 std::get() 不适用于变量定义的常量
- 元组的定义和初始化,其组件属于同一模板化类,但具有不同的专用化
- 使用GCC v4.8在Ubuntu 14.04上定义C 11中元组向量时的编译错误
- 如何类型定义作为其他元组串联的元组
- 推力:redy_by_key将zip_iterator(元组)传递到自定义函子中,以通过键检索平均值
- 如何使用 C++11 可变参数模板来定义由向量元组支持的元组向量
- 没有 == 为提升::元组定义
- 比较具有替代排序的自定义类型的std::元组(或std::对).是否可以插入自定义的小于/比较函数
- 无法重载元组的模板运算符,但自定义类型正常
- 为不使用元组的自定义映射模拟std::map迭代器
- 从QSettings中读取自定义元类型数组
- 如何通过lambda使用自定义相等来查找在std::vector中具有一个匹配元素的元组
- 使用gtest和google sparsehash时重新定义元组
- c++元组链接问题:未定义的引用
- 自定义对元组向量进行排序
- 自定义排序元组向量时出错