c++ namespace collision with gtest and boost
-
18-09-2019 - |
Question
If I include both gtest/gtest.h and boost/math/distributions/poisson.hpp I get
/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
How do I prevent these two library namespaces from colliding?
Solution
Try building with BOOST_HAS_TR1_TUPLE
defined. It looks like both boost and your std
libraries are defining std::tr1::tuple
and I can't see how to disable the std
version. Looking at the boost header though it appears that BOOST_HAS_TR1_TUPLE
needs to be defined to tell boost that std::tr1::tuple
is already defined.
I got similar errors to yours when I tried to compile a file including both those headers and then they disappeared when I defined BOOST_HAS_TR1_TUPLE
.
OTHER TIPS
Have you tried switching the order of the includes? It is possible the other header file handles things a little more gracefully.
With gtest setting GTEST_HAS_TR1_TUPLE to 0 in gtest.h helped in my case
To be able to include boost/math/distributions/fisher_f.hpp in Microsoft Visual Studio 2010,
I needed to define BOOST_NO_0X_HDR_TUPLE
as well as BOOST_HAS_TR1_TUPLE
.