Ubuntu展示点云使用boost::this_thread报错
Ubuntu使用boost::this_thread展示点云
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
viewer->setBackgroundColor(180, 180, 180);
viewer->addCoordinateSystem(1.0);
viewer->initCameraParameters();
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZI> color_right(cloud_right, 255, 0, 0);
viewer->addPointCloud<pcl::PointXYZI>(cloud_right, color_right, "cloud_right");
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZI> color_left(cloud_left, 0, 0, 255);
viewer->addPointCloud<pcl::PointXYZI>(cloud_left, color_left, "cloud_left");
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep (boost::posix_time::microseconds (100000));
//boost::thread::sleep(boost::get_system_time() + boost::posix_time::seconds(5));
}
return;
头文件没有加boost的任何头文件,make时报错boost::this_thread’ has not been declared
:
/home/qiancj/Desktop/ShareFolder/5Codes/Cluster_ExtractCentrepoint/Cluster_ExtractCentrepoint.cc: In function ‘int main(int, char**)’:
/home/qiancj/Desktop/ShareFolder/5Codes/Cluster_ExtractCentrepoint/Cluster_ExtractCentrepoint.cc:277:16: error: ‘boost::this_thread’ has not been declared
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
^~~~~~~~~~~
CMakeFiles/Cluster_ExtractCentrepoint.dir/build.make:62: recipe for target 'CMakeFiles/Cluster_ExtractCentrepoint.dir/Cluster_ExtractCentrepoint.cc.o' failed
make[2]: *** [CMakeFiles/Cluster_ExtractCentrepoint.dir/Cluster_ExtractCentrepoint.cc.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/Cluster_ExtractCentrepoint.dir/all' failed
make[1]: *** [CMakeFiles/Cluster_ExtractCentrepoint.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
头文件中增加boost的头文件
#include <boost/thread/thread.hpp>
make时报错undefined reference to boost::this_thread::hidden::sleep_until(timespec const&)
:
CMakeFiles/Cluster_ExtractCentrepoint.dir/Cluster_ExtractCentrepoint.cc.o: In function `boost::this_thread::sleep(boost::posix_time::ptime const&)':
Cluster_ExtractCentrepoint.cc:(.text._ZN5boost11this_thread5sleepERKNS_10posix_time5ptimeE[_ZN5boost11this_thread5sleepERKNS_10posix_time5ptimeE]+0x37): undefined reference to `boost::this_thread::hidden::sleep_until(timespec const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/Cluster_ExtractCentrepoint.dir/build.make:197: recipe for target 'Cluster_ExtractCentrepoint' failed
make[2]: *** [Cluster_ExtractCentrepoint] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/Cluster_ExtractCentrepoint.dir/all' failed
make[1]: *** [CMakeFiles/Cluster_ExtractCentrepoint.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
Method 1
需要在cmake之后,修改生成的项目.dir
目录下link.txt文件:
在文件最后增加
-lboost_thread
Method 2
方法1就是添加了boost_thread的链接,所以直接在CmakeLists.txt
文件中添加即可
find_package(Boost 1.65.1 REQUIRED COMPONENTS thread) # 我的boost版本是1.65.1的
IF (Boost_FOUND)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
Message(STATUS, "PRINT BOOST: ${Boost_INCLUDE_DIR}")
Message(STATUS, "PRINT BOOST: ${Boost_THREAD_LIBRARY}")
ADD_DEFINITIONS( "-DHAS_BOOST" )
ENDIF()
target_link_libraries (ProjectName ${Boost_THREAD_LIBRARY})
cmake之后,再打开link.txt文件,发现新增了/usr/lib/x86_64-linux-gnu/libboost_thread.so
动态库
编译成功:
以上。