我打算使用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 。搞定!