记录一次编译错误
boost::this_thread 报错问题
使用boost::this_thread时,
代码:
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
提示
No member named ‘this_thread’ in namespace ‘boost’ No member named ‘posix_time’ in namespace ‘boost’
解决办法:
包含头文件
#include <boost/thread/thread.hpp>
然后没有错误提示了,但是编译时,又报错
error: LNK2019: 无法解析的外部符号 "bool __cdecl boost::this_thread::interruptible_wait
解决办法
将
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
替换为
std::this_thread::sleep_for(std::chrono::microseconds(100000));
包含头文件
#include <chrono>
#include <thread>
原因分析
之前只是写了解决办法,没有分析原因,并且有网友提问,于是补充原因,
首先贴出错误
error LNK2019: 无法解析的外部符号 "bool __cdecl boost::this_thread::interruptible_wait(void *,struct boost::detail::mono_platform_timepoint const &)"
这种错误大概率是没有链接到,但是boost其他一些库是能用的,因此不可能是整个boost没有链接到,于是排查pcl库的编译配置文件 PCLConfig.cmake,
在配置文件中发现pcl并没有链接boost的thread库,
于是做如下修改,
在PCL安装目录(Cmake环境)如 PCL1.12.1\cmake 下找到 PCLConfig.cmake文件
然后在 PCLConfig.cmake文件中找到如下代码
find_package(Boost 1.65.0 ${
QUIET_} COMPONENTS system filesystem date_time iostreams serialization)
加上thread,修改后如下
find_package(Boost 1.65.0 ${
QUIET_} COMPONENTS system filesystem date_time iostreams serialization thread)
保存后,回到项目代码,包含头文件
#include <boost/thread/thread.hpp>
调用 boost::this_thread测试,代码如下,
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
编译通过
到这里虽然问题解决并找到原因了,
但还是疑惑,为什么pcl没有依赖boost thread,于是查看了pcl官方源码提交记录,发现在pcl 1.10.0版本之前是有依赖thread的,但是从pcl 1.10.0开始移除了 对thread的依赖,
移除日志截图,
移除原因大概是因为在c++新标准之前没有提供多线程,因此pcl依赖了boost的thread,在c++支持新标准后,可直接使用c++新特性,便移除了boost thread吧。