Debug:'boost::this_thread' has not been declared; undefined reference to 'boost::this_thread...'
今天解决两个C++编译工程时的BUG。
问题1描述
编译到某个.cpp文件时,报'boost::this_thread' has not been declared;
,这个很明显是这个.cpp文件没有找到boost
库里的这个函数的问题。
解决方法
在该.cpp文件或者对应的.h文件中,添加下面的头文件引用:
#include <boost/thread/thread.cpp>
再次编译时,就不会再报这个错误了。
问题2描述
编译完所有文件,开始链接时,报undefined reference to 'boost::this::hidden::sleep_until(timespec const&)'
,这个也是老错误了,多半是CMakeLists.txt文件里没有配置boost库,或者没有把boost库链接到target上。
解决方法
在CMakeLists.txt中添加配置:
find_package(Boost REQUIRED COMPONENTS thread)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIRS})
add_definitions(-DBOOST_ALL_DYN_LINK)
// 要把上面的库链接到可执行文件
target_link_libraries(main ${Boost_LIBRARIES})
再次编译链接,就不会报这个错了。