C++ boost usage

本文介绍了Boost库的多种安装方法,包括从源代码构建、使用MacPorts安装等。针对不同的操作系统和编译器提供了详细的步骤说明。

Install BOOST

1. Build from source code

Download the new release package from boost official website

For windows, download zip or 7z format and unpack it using 7zip or winrar

For linux/mac os, download tar.gz or tar.bz2, and unpack it by these command.

tar zxf ....tar.gz -C ./ or tar jxf ...tar.bz2 -C ./

cd the boost_1_xx_x

for windows,  run bootstrap.bat to create bjam and b2

for linux/mac os, run ./bootstrap.sh to create bjam and b2

build the library for different compiler: 

for msvc

mkdir msvc # store the msvc libraries

./bjam toolset=msvc-10.0 --build-dir=msvc --build-type=complete --stagedir=msvc --with-filesystem --disablefilesystem2

for gcc

mkdir gcc # store the gcc libraries

./bjam toolset=gcc --build-dir=gcc --build-type=complete --stagedir=gcc --with-filesystem --disablefilesytem2 --layout=versioned

for darwin

mkdir darwin # store the darwin libraries

./bjam toolset=darwin --build-dir=darwin --build-type=complete --stagedir=darwin --with-filsystem --disablefilesystem2 --layout=versioned

2. Use macport to install

download the macport from officail website and then install it.

Run these command to install it.

sudo port selfupdate #update the software tree

port search boost #check whether contains boost

port info boost #check details about boost

sudo port install boost

the boost will be installed under /opt/local/include for header file, /opt/local/ilb for the library. If you want to change the path, need to build the source code using --prefix.


好的,以下是一个简单的使用 boost::asio 库编写的服务器示例: ```c++ #include <iostream> #include <boost/asio.hpp> using namespace boost::asio; using namespace boost::asio::ip; class Session : public std::enable_shared_from_this<Session> { public: Session(tcp::socket socket) : socket_(std::move(socket)) {} void start() { do_read(); } private: void do_read() { auto self(shared_from_this()); socket_.async_read_some(buffer(data_, max_length), [this, self](boost::system::error_code ec, std::size_t length) { if (!ec) { do_write(length); } }); } void do_write(std::size_t length) { auto self(shared_from_this()); async_write(socket_, buffer(data_, length), [this, self](boost::system::error_code ec, std::size_t /*length*/) { if (!ec) { do_read(); } }); } tcp::socket socket_; enum { max_length = 1024 }; char data_[max_length]; }; class Server { public: Server(io_service& ios, short port) : acceptor_(ios, tcp::endpoint(tcp::v4(), port)), socket_(ios) { do_accept(); } private: void do_accept() { acceptor_.async_accept(socket_, [this](boost::system::error_code ec) { if (!ec) { std::make_shared<Session>(std::move(socket_))->start(); } do_accept(); }); } tcp::acceptor acceptor_; tcp::socket socket_; }; int main(int argc, char* argv[]) { try { if (argc != 2) { std::cerr << "Usage: server <port>" << std::endl; return 1; } io_service ios; Server s(ios, std::atoi(argv[1])); ios.run(); } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << std::endl; } return 0; } ``` 这个服务器使用了 boost::asio 库提供的异步 I/O 操作实现,它只会在有客户端连接时才会创建一个新的 Session 对象处理该客户端的请求,这样可以避免阻塞主线程。在 Session 对象中,我们使用了 async_read_some 和 async_write 这两个异步操作来处理客户端的读写请求,这样可以保证多个客户端之间互不干扰。最后,在 main 函数中,我们启动了一个 Server 对象,并调用了 io_service 的 run 方法来启动事件循环。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值