我打算使用gdb调试pcl的kdtree教程(kdtree_search.cpp)。由于pcl经过预编译,将kdtree模块编译成动态链接库。使用gdb无法进入动态链接库进行调试!
因此将kdtree模块(主要为kdtree_flann.h和kdtree_flann.hpp)的相关代码抽取出来,单独编译。代码包的树状图如下所示,
上图可见,kdtree模块的依赖了这么多文件,可见pcl的代码抽象程度,重用性做得非常好。
附上CMakeLists.txt,
cmake_minimum_required(VERSION 2.8.3) project(kdtree_learn) set(CMAKE_BUILD_TYPE "DEBUG") add_definitions(-std=c++11) find_package(PkgConfig REQUIRED) pkg_check_modules(FLANN REQUIRED) include_directories( "/usr/include/eigen3" "/home/gordon/kdtree_learn/include/" ) add_executable(${PROJECT_NAME} kdtree_search.cpp)
运行make进行编译,报错如下,
CMakeFiles/kdtree_learn.dir/kdtree_search.cpp.o: In function `main': /home/gordon/kdtree_learn/kdtree_search.cpp:27: undefined reference to `pcl::KdTreeFLANN<pcl::PointXYZ, flann::L2_Simple<float> >::KdTreeFLANN(bool)' /home/gordon/kdtree_learn/kdtree_search.cpp:29: undefined reference to `pcl::KdTreeFLANN<pcl::PointXYZ, flann::L2_Simple<float> >::setInputCloud(boost::shared_ptr<pcl::PointCloud<pcl::PointXYZ> const> const&, boost::shared_ptr<std::vector<int, std::allocator<int> > const> const&)' /home/gordon/kdtree_learn/kdtree_search.cpp:49: undefined reference to `pcl::KdTreeFLANN<pcl::PointXYZ, flann::L2_Simple<float> >::nearestKSearch(pcl::PointXYZ const&, int, std::vector<int, std::allocator<int> >&, std::vector<float, std::allocator<float> >&) const' /home/gordon/kdtree_learn/kdtree_search.cpp:71: undefined reference to `pcl::KdTreeFLANN<pcl::PointXYZ, flann::L2_Simple<float> >::radiusSearch(pcl::PointXYZ const&, double, std::vector<int, std::allocator<int> >&, std::vector<float, std::allocator<float> >&, unsigned int) const' CMakeFiles/kdtree_learn.dir/kdtree_search.cpp.o: In function `pcl::KdTreeFLANN<pcl::PointXYZ, flann::L2_Simple<float> >::~KdTreeFLANN()': /home/gordon/kdtree_learn/include/pcl/kdtree/kdtree_flann.h:140: undefined reference to `pcl::KdTreeFLANN<pcl::PointXYZ, flann::L2_Simple<float> >::cleanup()' CMakeFiles/kdtree_learn.dir/kdtree_search.cpp.o:(.rodata._ZTVN3pcl11KdTreeFLANNINS_8PointXYZEN5flann9L2_SimpleIfEEEE[_ZTVN3pcl11KdTreeFLANNINS_8PointXYZEN5flann9L2_SimpleIfEEEE]+0x10): undefined reference to `pcl::KdTreeFLANN<pcl::PointXYZ, flann::L2_Simple<float> >::setInputCloud(boost::shared_ptr<pcl::PointCloud<pcl::PointXYZ> const> const&, boost::shared_ptr<std::vector<int, std::allocator<int> > const> const&)' CMakeFiles/kdtree_learn.dir/kdtree_search.cpp.o:(.rodata._ZTVN3pcl11KdTreeFLANNINS_8PointXYZEN5flann9L2_SimpleIfEEEE[_ZTVN3pcl11KdTreeFLANNINS_8PointXYZEN5flann9L2_SimpleIfEEEE]+0x28): undefined reference to `pcl::KdTreeFLANN<pcl::PointXYZ, flann::L2_Simple<float> >::nearestKSearch(pcl::PointXYZ const&, int, std::vector<int, std::allocator<int> >&, std::vector<float, std::allocator<float> >&) const' CMakeFiles/kdtree_learn.dir/kdtree_search.cpp.o:(.rodata._ZTVN3pcl11KdTreeFLANNINS_8PointXYZEN5flann9L2_SimpleIfEEEE[_ZTVN3pcl11KdTreeFLANNINS_8PointXYZEN5flann9L2_SimpleIfEEEE]+0x40): undefined reference to `pcl::KdTreeFLANN<pcl::PointXYZ, flann::L2_Simple<float> >::radiusSearch(pcl::PointXYZ const&, double, std::vector<int, std::allocator<int> >&, std::vector<float, std::allocator<float> >&, unsigned int) const' CMakeFiles/kdtree_learn.dir/kdtree_search.cpp.o:(.rodata._ZTVN3pcl11KdTreeFLANNINS_8PointXYZEN5flann9L2_SimpleIfEEEE[_ZTVN3pcl11KdTreeFLANNINS_8PointXYZEN5flann9L2_SimpleIfEEEE]+0x58): undefined reference to `pcl::KdTreeFLANN<pcl::PointXYZ, flann::L2_Simple<float> >::setEpsilon(float)' collect2: error: ld returned 1 exit status
这个报错最头疼的地方在于,明明所有文件都已到齐,然而就是 undefined reference to '......'。
经过一番排查,发现问题出在 kdtree_flann.h 中的一小段预编译代码,
#ifdef PCL_NO_PRECOMPILE #include <pcl/kdtree/impl/kdtree_flann.hpp> #endif
它的意思是:如果定义了宏,则表示这份代码不进行预编译,那么要添加 kdtree_flann.hpp 。
kdtree_flann.h 声明了 KdTreeFLANN 类,kdtree_flann.hpp 定义了类内的成员实现。
由于我把这部分代码抽离出来单独编译,也即是没有预编译,所以没有添加特定路径下的 kdtree_flann.hpp ,进而出现上述报错。
解决方案:
在 kdtree_flann.h 中添加 #define PCL_NO_PRECOMPILE 。搞定!