visual studio 是写 c++ 最好的 IDE 这是宇宙真理没有什么好讨论了,加上 VAssistX 更是如虎添翼。哪怕是 linux 开发我都要搭一个 windows->linux 的文件自动实时同步环境,然后在 windows 上编码,上 linux 去 make。
但是,问题来了,如何让 visual studio 搭配 boost 呢。
找了一下资料,例如,http://blog.csdn.net/lanyzh0909/article/details/7227839,http://zjhwl.iteye.com/blog/1358495,http://redwolf.blog.51cto.com/427621/88403,都提到要自行编译,我勒个去,boost 那个庞然大物,编译完了天都黑了,于是找啊找,找到这里,http://stackoverflow.com/questions/2049952/how-to-get-boost-libraries-binaries-that-work-with-visual-studio,http://boost.teeks99.com/,有人编译好了,直接下载回来,就可以用了
解压缩出来,把 hpp 和 lib 库加到 visual studio 的引用路径配置中,就可以了
还剩下最后一个问题,VAssistX,看到这里,http://blog.csdn.net/yunccll/article/details/7219778,把配置配好,也 OK,全程很顺利,没有卡壳点。
然后就是好好用 boost 了。
———————
2015-8-4 10:47:13 附上一个 windows 下能跑的 boost asio 样例,抄这里的,http://zh.highscore.de/cpp/boost/asio.html
#define _WIN32_WINNT 0x0501
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/array.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
#include <string.h>
#include <string>
#define _PRINTF_(format, ...) printf("[%s] " format " [%s::%s::%d]\n", boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time()).c_str(), ##__VA_ARGS__, (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__), __FUNCTION__, __LINE__)
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::socket sock(io_service);
boost::array<char, 4096> buffer;
void timer_handler(const boost::system::error_code &ec)
{
_PRINTF_("5 s.");
}
void read_handler(const boost::system::error_code& ec, std::size_t bytes_transferred)
{
if (ec)
{
_PRINTF_("%s", ec.message().c_str());
return;
}
_PRINTF_("read success [%s]", std::string(buffer.data(), bytes_transferred).c_str());
sock.async_read_some(boost::asio::buffer(buffer), boost::bind(read_handler, _1, _2));
}
void connect_handler(const boost::system::error_code& ec)
{
if (ec)
{
_PRINTF_("%s", ec.message().c_str());
return;
}
_PRINTF_("connect success");
boost::asio::write(sock, boost::asio::buffer("GET /404/ HTTP/1.1\r\nHost: www.qq.com\r\n\r\n"));
sock.async_read_some(boost::asio::buffer(buffer), boost::bind(read_handler, _1, _2));
}
void resolver_handler(const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator it)
{
if (ec)
{
_PRINTF_("%s", ec.message().c_str());
return;
}
_PRINTF_("resolver success");
sock.async_connect(*it, boost::bind(connect_handler, _1));
}
int main()
{
boost::asio::deadline_timer timer(io_service, boost::posix_time::seconds(5));
timer.async_wait(boost::bind(timer_handler, _1));
boost::asio::ip::tcp::resolver::query query("www.qq.com", "80");
resolver.async_resolve(query, boost::bind(resolver_handler, _1, _2));
io_service.run();
return 0;
}
#define _WIN32_WINNT 0x0501
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/array.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
#include <string.h>
#include <string>
#define _PRINTF_(format, ...) printf("[%s] " format " [%s::%s::%d]\n", boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time()).c_str(), ##__VA_ARGS__, (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__), __FUNCTION__, __LINE__)
boost::asio::io_service io_service;
//boost::asio::ip::tcp::resolver resolver(io_service);
//boost::asio::ip::tcp::socket sock(io_service);
//boost::array<char, 4096> buffer;
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 8080);
boost::asio::ip::tcp::acceptor acceptor(io_service, endpoint);
void timer_handler(const boost::system::error_code &ec)
{
_PRINTF_("5 s.");
}
/*
void read_handler(const boost::system::error_code& ec, std::size_t bytes_transferred)
{
if (ec)
{
_PRINTF_("%s", ec.message().c_str());
return;
}
_PRINTF_("read success [%s]", std::string(buffer.data(), bytes_transferred).c_str());
sock.async_read_some(boost::asio::buffer(buffer), boost::bind(read_handler, _1, _2));
}
void connect_handler(const boost::system::error_code& ec)
{
if (ec)
{
_PRINTF_("%s", ec.message().c_str());
return;
}
_PRINTF_("connect success");
boost::asio::write(sock, boost::asio::buffer("GET /404/ HTTP/1.1\r\nHost: www.qq.com\r\n\r\n"));
sock.async_read_some(boost::asio::buffer(buffer), boost::bind(read_handler, _1, _2));
}
void resolver_handler(const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator it)
{
if (ec)
{
_PRINTF_("%s", ec.message().c_str());
return;
}
_PRINTF_("resolver success");
sock.async_connect(*it, boost::bind(connect_handler, _1));
}*/
void start_accept();
void write_handler(const boost::system::error_code &ec, std::size_t bytes_transferred, boost::shared_ptr<boost::asio::ip::tcp::socket> clientSocket, boost::shared_ptr<std::string> responseDataPtr)
{
if (ec)
{
_PRINTF_("%s", ec.message().c_str());
return;
}
_PRINTF_("write %d bytes success to [%s:%d]", bytes_transferred, clientSocket->remote_endpoint().address().to_string().c_str(), clientSocket->remote_endpoint().port());
}
void accept_handler(const boost::system::error_code &ec, boost::shared_ptr<boost::asio::ip::tcp::socket> clientSocket)
{
if (ec)
{
_PRINTF_("%s", ec.message().c_str());
return;
}
_PRINTF_("accept success, [%s:%d]", clientSocket->remote_endpoint().address().to_string().c_str(), clientSocket->remote_endpoint().port());
start_accept();
std::string responseContent = "<h1>Hello, world</h1><p>" + boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time()) + "</p>";
std::string data = "HTTP/1.1 200 OK\r\nContent-Length: " + boost::lexical_cast<std::string>(responseContent.length()) + "\r\n\r\n" + responseContent;
boost::shared_ptr<std::string> responseDataPtr = boost::make_shared<std::string>(data);
boost::asio::async_write(*clientSocket, boost::asio::buffer(*responseDataPtr), boost::bind(write_handler, _1, _2, clientSocket, responseDataPtr));
}
void start_accept()
{
//boost::shared_ptr<boost::asio::ip::tcp::socket> clientSocket = boost::make_shared<boost::asio::ip::tcp::socket>(io_service);
boost::shared_ptr<boost::asio::ip::tcp::socket> clientSocket(new boost::asio::ip::tcp::socket(io_service));
acceptor.async_accept(*clientSocket, boost::bind(accept_handler, _1, clientSocket));
}
int main()
{
boost::asio::deadline_timer timer(io_service, boost::posix_time::seconds(5));
timer.async_wait(boost::bind(timer_handler, _1));
//boost::asio::ip::tcp::resolver::query query("www.qq.com", "80");
//resolver.async_resolve(query, boost::bind(resolver_handler, _1, _2));
acceptor.listen();
_PRINTF_("listening...");
start_accept();
io_service.run();
return 0;
}
#define _WIN32_WINNT 0x0501
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/ref.hpp>
#include <boost/make_shared.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/array.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
#include <string.h>
#include <string>
#include <ctime>
#define _PRINTF_(format, ...) printf("[%s] " format " [%s::%s::%d]\n", boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time()).c_str(), ##__VA_ARGS__, (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__), __FUNCTION__, __LINE__)
boost::asio::io_service io_service;
//boost::asio::ip::tcp::resolver resolver(io_service);
//boost::asio::ip::tcp::socket sock(io_service);
//boost::array<char, 4096> buffer;
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 80);
boost::asio::ip::tcp::acceptor acceptor(io_service, endpoint);
void timer_handler(const boost::system::error_code &ec, boost::asio::deadline_timer &timer)
{
_PRINTF_("5 s.");
timer.expires_at(timer.expires_at() + boost::posix_time::seconds(5));
timer.async_wait(boost::bind(timer_handler, _1, boost::ref(timer)));
}
/*
void read_handler(const boost::system::error_code& ec, std::size_t bytes_transferred)
{
if (ec)
{
_PRINTF_("%s", ec.message().c_str());
return;
}
_PRINTF_("read success [%s]", std::string(buffer.data(), bytes_transferred).c_str());
sock.async_read_some(boost::asio::buffer(buffer), boost::bind(read_handler, _1, _2));
}
void connect_handler(const boost::system::error_code& ec)
{
if (ec)
{
_PRINTF_("%s", ec.message().c_str());
return;
}
_PRINTF_("connect success");
boost::asio::write(sock, boost::asio::buffer("GET /404/ HTTP/1.1\r\nHost: www.qq.com\r\n\r\n"));
sock.async_read_some(boost::asio::buffer(buffer), boost::bind(read_handler, _1, _2));
}
void resolver_handler(const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator it)
{
if (ec)
{
_PRINTF_("%s", ec.message().c_str());
return;
}
_PRINTF_("resolver success");
sock.async_connect(*it, boost::bind(connect_handler, _1));
}*/
void start_accept();
void write_handler(const boost::system::error_code &ec, std::size_t bytes_transferred, boost::shared_ptr<boost::asio::ip::tcp::socket> clientSocket, boost::shared_ptr<std::string> responseDataPtr)
{
if (ec)
{
_PRINTF_("%s", ec.message().c_str());
return;
}
_PRINTF_("write %d bytes success to [%s:%d]", bytes_transferred, clientSocket->remote_endpoint().address().to_string().c_str(), clientSocket->remote_endpoint().port());
}
void receive_handler(const boost::system::error_code &ec, std::size_t bytes_transferred, boost::shared_ptr<boost::asio::ip::tcp::socket> clientSocket, boost::shared_ptr<boost::array<char, 4096> > receiveBuffer)
{
if (ec)
{
_PRINTF_("%s, [%s:%d]", ec.message().c_str(), clientSocket->remote_endpoint().address().to_string().c_str(), clientSocket->remote_endpoint().port());
return;
}
_PRINTF_("receive %d bytes success from [%s:%d]", bytes_transferred, clientSocket->remote_endpoint().address().to_string().c_str(), clientSocket->remote_endpoint().port());
std::string receivedContent(receiveBuffer->begin(), receiveBuffer->end());
if (receivedContent.find("\r\n\r\n") != std::string::npos)
{
_PRINTF_("[%s:%d] %s", clientSocket->remote_endpoint().address().to_string().c_str(), clientSocket->remote_endpoint().port(), receivedContent.c_str());
std::string responseContent = "<h1>Hello, world</h1><p>" + boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time()) + "</p>";
std::string responseData = "HTTP/1.1 200 OK\r\nContent-Length: " + boost::lexical_cast<std::string>(responseContent.length()) + "\r\n\r\n" + responseContent;
boost::shared_ptr<std::string> responseDataPtr = boost::make_shared<std::string>(responseData);
//boost::asio::async_write(*clientSocket, boost::asio::buffer(*responseDataPtr), boost::bind(write_handler, _1, _2, clientSocket, responseDataPtr));
clientSocket->async_send(boost::asio::buffer(*responseDataPtr), boost::bind(write_handler, _1, _2, clientSocket, responseDataPtr));
}
clientSocket->async_receive(boost::asio::buffer(*receiveBuffer), boost::bind(receive_handler, _1, _2, clientSocket, receiveBuffer));
}
void accept_handler(const boost::system::error_code &ec, boost::shared_ptr<boost::asio::ip::tcp::socket> clientSocket)
{
if (ec)
{
_PRINTF_("%s", ec.message().c_str());
return;
}
_PRINTF_("accept success, [%s:%d]", clientSocket->remote_endpoint().address().to_string().c_str(), clientSocket->remote_endpoint().port());
start_accept();
boost::shared_ptr<boost::array<char, 4096> > receiveBuffer(new boost::array<char, 4096>());
clientSocket->async_receive(boost::asio::buffer(*receiveBuffer), boost::bind(receive_handler, _1, _2, clientSocket, receiveBuffer));
}
void start_accept()
{
//boost::shared_ptr<boost::asio::ip::tcp::socket> clientSocket = boost::make_shared<boost::asio::ip::tcp::socket>(io_service);
boost::shared_ptr<boost::asio::ip::tcp::socket> clientSocket(new boost::asio::ip::tcp::socket(io_service));
acceptor.async_accept(*clientSocket, boost::bind(accept_handler, _1, clientSocket));
}
int main()
{
boost::asio::deadline_timer timer(io_service, boost::posix_time::seconds(5));
//timer.async_wait(boost::bind(timer_handler, _1, boost::ref(timer)));
//boost::asio::ip::tcp::resolver::query query("www.qq.com", "80");
//resolver.async_resolve(query, boost::bind(resolver_handler, _1, _2));
acceptor.listen();
_PRINTF_("listening...");
start_accept();
io_service.run();
return 0;
}