Boost 使用

1. 下载编译

   下载 boost-1.53.0, 解压至E:/boost

   进入boost目录,运行booststrap.bat,会在当前目录生成 b2.exe, bjam.exe 两个可执行文件,编译boost需要执行bjam命令

 

2. 编译boost

   显示所有库名称

   bjam --show-libraries 

 

   编译命令

rem 编译静态库
bjam stage --without-python --toolset=msvc-10.0 --build-type=complete --stagedir="E:\boost\bin" link=static runtime-link=static threading=multi debug release

rem 编译动态库
bjam stage --without-python --toolset=msvc-10.0 --build-type=complete --stagedir="E:\boost\bin" link=shared runtime-link=shared threading=multi debug release

 

   参数: 

   stagedir 编译后生成文件存放路径

   link: 生成静态/动态库,一般生成静态库

   threading: 指定multi,多线程编译

   等待半个多小时后,编译完成,会在E:/boost目录生成bin.v2(中间目录,可删除),和bin目录. bin/lib目录下存放的是debug/release版本的静态库

 

3. 测试

    vs10下新建win32控制台项目, 设置附加库目录: E:/boost  附加库目录: E:/boost/bin/lib 

 

#include <iostream>
#include <string>

// 此宏抑制boost自动链接,否则会自动链接boost_regex_xxx.lib这样名称的库
#define BOOST_ALL_NO_LIB

#include <boost/regex.hpp>

#pragma comment(lib,"libboost_regex-vc100-mt-sgd-1_53.lib")

/**
 * 测试Boost正则表达式库 
 *
 */
int main(int argc,char **argv)
{
   std::string line;
   boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );

    while (std::cin)
    {
        std::getline(std::cin, line);
        boost::smatch matches;
        if (boost::regex_match(line, matches, pat))
	{
            std::cout << matches[2] << std::endl;
	}
    }

	return 0;
}

 

    编译出现链接错误:

    1>libboost_regex-vc100-mt-sgd-1_53.lib(instances.obj) : error LNK2005: "public: __thiscall std::_Container_base12::_Container_base12(void)" (??0_Container_base12@std@@QAE@XZ) 已经在 msvcprtd.lib(MSVCP100D.dll) 中定义

    1>libboost_regex-vc100-mt-sgd-1_53.lib(instances.obj) : error LNK2005: "public: __thiscall std::_Container_base12::~_Container_base12(void)" (??1_Container_base12@std@@QAE@XZ) 已经在 msvcprtd.lib(MSVCP100D.dll) 中定义

   1>libboost_regex-vc100-mt-sgd-1_53.lib(regex_traits_defaults.obj) : error LNK2005: "public: void __thiscall std::_Container_base12::_Orphan_all(void)" (?_Orphan_all@_Container_base12@std@@QAEXXZ) 已经在 msvcprtd.lib(MSVCP100D.dll) 中定义

   1>LINK : warning LNK4098: 默认库“LIBCMTD”与其他库的使用冲突;请使用 /NODEFAULTLIB:library

   1>F:\vs workspace\boost\Debug\boost.exe : fatal error LNK1169: 找到一个或多个多重定义的符号

 

    解决方法:  msvcprtd.lib表示要用静态库,所以在生成要链接到这个项目的lib时,设置 工程/属性/C/C++/代码生成/运行库 /MTd,重新编译,一切OK

    

4. 其它平台的编译

    Linux平台下编译与上面类似,而且更简单,三条命令搞定:

    ./boostrap.sh    # 产生b2,bjam可执行文件

    sudo ./b2

    sudo ./b2 install  # 安装,头文件和库默认安装在 /usr/local 下

 

5. asio 

    相对于老牌的通信框架ACE, boost asio显示更加轻量级,与libevent一样,底层也封装了高效的网络模型, 内部通过对线程队列的实现,也使得该框架性能也是非常优良. boost 内部实现产生了大量的宏和模板,编程感觉有点不习惯,下面看看 asio 异步TCP服务器的实现源码:

// TCP 服务器
class TcpServer
{
public:
	TcpServer(boost::asio::io_service &io_service)
	  :io_service(io_service)
	  ,acceptor_(io_service,tcp::endpoint(tcp::v4(),1234))   // 监听1234号端口
	{
		// 等待连接
		start();
	}

	void start()
	{
		boost::shared_ptr<tcp::socket> psocket(new tcp::socket(io_service));

		// 注册连接事件回调函数
		acceptor_.async_accept(*psocket,
			boost::bind(&TcpServer::handle_accept,
			this,
			psocket,
			boost::asio::placeholders::error)
			);
	}

	void handle_accept(boost::shared_ptr<tcp::socket> psocket,const boost::system::error_code &error)
	{
		if(error)	return;

		// 显示远程ip
		std::cout<<psocket->remote_endpoint().address()<<"---"<<psocket->remote_endpoint().port()<<std::endl;

		// 继续等待连接
		start();

		// 异步发送消息,async_ 开头的都是异步调用,不管回调是否结束,立即返回
		// 通过bind绑定参数,这个 async_write + bind 的调用形式比较奇怪
		// bind 指定回调函数 handle_write(),它需要两个参数 error,bytes_transferred
		message = "hello client";
		boost::asio::async_write(*psocket,boost::asio::buffer(message),
			boost::bind(&TcpServer::handle_write,this,boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred)
			);
	}

	void handle_write(const boost::system::error_code ec,size_t)
	{
		if(ec)
			printf("Fail to send!\n");
	}

private:
	boost::asio::io_service &io_service;
	tcp::acceptor acceptor_;
	std::string message;  
};


int main()
{
	boost::asio::io_service io_service;

	TcpServer server(io_service);
	io_service.run();   // 循环分发回调函数
	
	return 0;
}

猜你喜欢

转载自tcspecial.iteye.com/blog/1839105