linux下安装boost

1、下载解压boost包
2、运行boost目录下启动安装脚本:
./bootstrap.sh --with-libraries=all --with-toolset=gcc
说明:--with-libraries指定需要编译的库,all表示编译所有boost库;--with-toolset指定编译器
没有报错则安装正常
3、编译
./b2 toolset=gcc
4、安装
./b2 install --prefix=/usr
说明:--prefix指定boost安装路径,/usr表示boost安装到系统头文件和库路径,可以省略环境变量

5、最后运行ldconfig命令

6、测试

#include <boost/thread/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
#include <cstdlib>

using namespace std;
//using namespace boost; //为什么此处声明了会导致system编译报错

bool isRuning = true;
int cnt = 0;
boost::mutex mtx_cnt;


void thread_proc1()
{
	while( isRuning )
	{
		mtx_cnt.lock();
		std::cout << "thread1 cnt: " << ++cnt << std::endl;
		mtx_cnt.unlock();
		boost::this_thread::sleep( boost::posix_time::milliseconds( 500 ) );
		//sleep( 2 );
	}
	std::cout << "thread1 end" << std::endl;
}


void thread_proc2()
{
	while( isRuning )
	{
		mtx_cnt.lock();
		std::cout << "thread2 cnt: " << ++cnt << std::endl;
		mtx_cnt.unlock();
		boost::this_thread::sleep( boost::posix_time::milliseconds( 500 ) );
		//sleep( 2 );
	}
	std::cout << "thread2 end" << std::endl;
}
int main( int argc, char* argv[] )
{
	std::cout << "start" << std::endl;
	boost::thread thread1( &thread_proc1 );
	boost::thread thread2( &thread_proc2 );
	
	system( "read" );
	
	isRuning = false;
	thread1.join();
	thread2.join();
	
	std::cout << "exit" << std::endl;
	return 0;
}

编译:g++ main.cpp -o main -lboost_thread

./main 运行正常



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值