Boost、OpenCV 和 Eigen 库之间有冲突吗?

8

我的问题与在Eclipse CDR中静态链接Boost和OpenCV库时出现错误有关,而我想做的比这里描述的更多:如何使用Boost和OpenCV创建一个可以读取文件夹中所有图像的程序?,也就是使用Boost的文件系统库遍历一个目录,并对其中的图片文件进行一些OpenCV处理。

我使用MinGW编译了filesystem等库,并尝试在Windows 7 64位系统上使用Eclipse CDT运行Boost 1.45、OpenCV 2.2和Eigen2。如果单独在项目中使用文件系统库,则可以编译和运行,但与上述两个库结合使用时,会出现以下错误:

In file included from C:\boost_1_45_0/boost/filesystem/v3/path_traits.hpp:22:0, 
                 from C:\boost_1_45_0/boost/filesystem/v3/path.hpp:25, 
                 from C:\boost_1_45_0/boost/filesystem.hpp:32, 
                 from ..\src\ComputeNatScaleFunction.cpp:18: 
C:\boost_1_45_0/boost/type_traits/decay.hpp: In instantiation of 'boost::decay<cv::<anonymous enum> >': 
C:\cmake_binaries\include/opencv2/core/operations.hpp:766:23:   instantiated from here 
C:\boost_1_45_0/boost/type_traits/decay.hpp:28:66: error: 'cv::' is/uses anonymous type 
C:\boost_1_45_0/boost/type_traits/decay.hpp:28:66: error:   trying to instantiate 'template struct boost::remove_reference' 
C:\boost_1_45_0/boost/type_traits/decay.hpp:38:17: error: 'cv::' is/uses anonymous type 
C:\boost_1_45_0/boost/type_traits/decay.hpp:38:17: error:   trying to instantiate 'template struct boost::remove_reference' 
C:\boost_1_45_0/boost/type_traits/decay.hpp: In instantiation of 'boost::decay<cv::<anonymous enum> >': 
C:\cmake_binaries\include/opencv2/core/operations.hpp:917:21:   instantiated from here 
C:\boost_1_45_0/boost/type_traits/decay.hpp:28:66: error: 'cv::' is/uses anonymous type 
C:\boost_1_45_0/boost/type_traits/decay.hpp:28:66: error:   trying to instantiate 'template struct boost::remove_reference' 
C:\boost_1_45_0/boost/type_traits/decay.hpp:38:17: error: 'cv::' is/uses anonymous type 
C:\boost_1_45_0/boost/type_traits/decay.hpp:38:17: error:   trying to instantiate 'template struct boost::remove_reference' 
C:\boost_1_45_0/boost/type_traits/decay.hpp: In instantiation of 'boost::decay<Eigen::<anonymous enum> >': 
C:\Eigen2/Eigen/src/Core/GenericPacketMath.h:116:18:   instantiated from here 
C:\boost_1_45_0/boost/type_traits/decay.hpp:28:66: error: 'Eigen::' is/uses anonymous type 
C:\boost_1_45_0/boost/type_traits/decay.hpp:28:66: error:   trying to instantiate 'template struct boost::remove_reference' 
C:\boost_1_45_0/boost/type_traits/decay.hpp:38:17: error: 'Eigen::' is/uses anonymous type 
C:\boost_1_45_0/boost/type_traits/decay.hpp:38:17: error:   trying to instantiate 'template struct boost::remove_reference' 

任何提示可以解释这些库之间为什么会发生冲突吗?编译器无法通过包含文件系统(即第18行)的步骤。
1个回答

9
在包含Eigen之前使用boost::filesystem命名空间会导致编译器失败:
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
#include <Eigen/Core>

失败了,但是

#include <boost/filesystem.hpp>
#include <Eigen/Core>
using namespace boost::filesystem;

原因是如果将boost::filesystem添加到全局命名空间中,会污染它,并导致依赖于未污染命名空间的代码(例如:eigen)在编译期间出现错误。这并不奇怪。通常情况下,在包含文件之前不应该加入"using"语句。

工作正常。


4
你可以使用类似于namespace bfs = boost::filesystem;的语句来减少输入的类型负担 ";)"。 - rubenvb
很奇怪,但这确实解决了。谢谢! - vkotor
3
通常情况下,在任何using指令之前,您应始终包含所有头文件。在您的情况下,您正在将许多符号引入全局命名空间,这可能会“混淆”Eigen。 - Ferdinand Beyer
@Greystache, @vkotor:使用 "using namespace boost::filesystem" 将 boost::filesystem 中的所有符号名称都导入到全局命名空间,因此如果 boost::filesystem 有一个名为 Foo 的类,而 Eigen 也有一个名为 Foo 的类,则它们会冲突,编译器将不知道使用哪个。 - Catskul

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