C++命名空间与gtest和boost的冲突

6
如果我同时包含 gtest/gtest.hboost/math/distributions/poisson.hpp,我会得到:
/opt/local/include/boost/tr1/tuple.hpp:63: error: ‘tuple’ is already declared in this scope
/opt/local/include/boost/tr1/tuple.hpp:67: error: ‘make_tuple’ is already declared in this scope
/opt/local/include/boost/tr1/tuple.hpp:68: error: ‘tie’ is already declared in this scope
/opt/local/include/boost/tr1/tuple.hpp:68: error: ‘tie’ is already declared in this scope
/opt/local/include/boost/tr1/tuple.hpp:68: error: ‘tie’ is already declared in this scope
/opt/local/include/boost/tr1/tuple.hpp:68: error: ‘tie’ is already declared in this scope
/opt/local/include/boost/tr1/tuple.hpp:68: error: ‘tie’ is already declared in this scope
/opt/local/include/boost/tr1/tuple.hpp:68: error: ‘tie’ is already declared in this scope
/opt/local/include/boost/tr1/tuple.hpp:68: error: ‘tie’ is already declared in this scope
/opt/local/include/boost/tr1/tuple.hpp:68: error: ‘tie’ is already declared in this scope
/opt/local/include/boost/tr1/tuple.hpp:68: error: ‘tie’ is already declared in this scope
/opt/local/include/boost/tr1/tuple.hpp:68: error: ‘tie’ is already declared in this scope
/opt/local/include/boost/tr1/tuple.hpp:72: error: ‘tuple_size’ is already declared in this scope
/opt/local/include/boost/tr1/tuple.hpp:73: error: ‘tuple_element’ is already declared in this scope

我该如何防止这两个库命名空间冲突?
4个回答

10
尝试定义BOOST_HAS_TR1_TUPLE并进行构建。看起来boost和您的std库都定义了std::tr1::tuple,我不知道如何禁用std版本。然而,查看boost头文件,似乎需要定义BOOST_HAS_TR1_TUPLE来告诉boost已经定义了std::tr1::tuple
当我尝试编译包含这两个头文件的文件时,出现了与您类似的错误,但当我定义了BOOST_HAS_TR1_TUPLE后,这些错误就消失了。

5
这取决于编译器版本和gtest版本。除非编译器为gcc 4+,否则GTest还会定义自己的元组。因此,在这种情况下,boost将使用gtest的实现,您可能不想要这样。但是您也可以通过将GTEST_USE_OWN_TR1_TUPLE设置为0,在gtest中使用boost元组。 - housemaister

2

你尝试过交换包含的顺序吗?可能另一个头文件会更优雅地处理问题。


不幸的是,将顺序从gtest/boost更改为boost/gtest会导致混乱。 - bias

2
在我的情况下,通过在gtest.h中将GTEST_HAS_TR1_TUPLE设置为0有所帮助。

1
为了在Microsoft Visual Studio 2010中包含boost/math/distributions/fisher_f.hpp,我需要定义BOOST_NO_0X_HDR_TUPLE以及BOOST_HAS_TR1_TUPLE

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接