boost学习-1

环境:ubuntu 12.04 

首先就是下载boost库,boost库安装有很多种方法。首先我采用的是直接 apt-get install boost-all-dev

在控制台上可输入 apt-cache search boost 查看boost相关的一系列库。 在这里我是全部安装的,之前是安装的boost-dev最后在编译多线程程序时提示boost::thread join()函数未定义。在链接的时候 -lboost_thread找不到,被迫又安装了boost-thread-dev,所以建议大家还是直接安装boost-all-dev(完全安装)

我的网速一般,大概安装了20分钟。


最后写一个测试程序:

#include <boost/lexical_cast.hpp>
#include <iostream>

int main()
{
  using boost::lexical_cast;
  int a= lexical_cast<int>("123456");
  double b = lexical_cast<double>("123.456");
  std::cout << a << std::endl;
  std::cout << b << std::endl;
  return 0;
}

  多线程测试程序:

#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>

void count(int id)
{
	for(int i = 0; i < 10 ; i++)
	{
		boost::mutex::scoped_lock lock(io_mutex);
		std::cout << id << ":" << i << std::endl;
		sleep(1);
	}
}

int main()
{

	boost::thread thrd1(boost::bind(&count,1));
	boost::thread thrd2(boost::bind(&count,2));
	thrd1.join();
	thrd2.join();
	return 0;
}

编译boost测试程序:g++ xx.cpp -o xx -lboost_thread

你可能感兴趣的:(boost学习-1)