1、环境:
ubuntu18.04 桌面版
2、boost_1_67_0.tar.bz2 程序包编译后安装(具体过程百度下非常容易实现)
3、测试代码
#include <boost/thread.hpp>
#include <iostream>
void task1(){
std::cout<<"this is task1!"<<std::endl;
}
void task2(){
std::cout<<"this is task2!"<<std::endl;
}
int main(int argc,char **argv){
using namespace boost;
thread thread_1=thread(task1);
thread thread_2=thread(task2);
boost::this_thread::sleep(boost::posix_time::seconds(2));
thread_2.join();
thread_1.join();
return 0;
}
编译指令:
g++ t1.cpp -I /usr/local/include/ -L /usr/local/lib/ -lboost_system -lboost_filesystem -lboost_thread -o t1
错误提示:
usr/bin/ld: /tmp/cccCDjBu.o: undefined reference to symbol 'pthread_condattr_setclock@@GLIBC_2.3.3'
//lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
修正后的编译指令:g++ t1.cpp -I /usr/local/include/ -L /usr/local/lib/ -lboost_system -lboost_filesystem -lboost_thread -lpthread -o t1
说明:
增加了 -lpthread 问题解决
ld找不到对应symbol不是由于对应so不在路径中,而是DSO missing from command line
. 链接阶段,指定使用的动态库:-lpthread