Windows MinGW 64-bit boost 踩坑

>g++ -Wall -shared -g -DBUILD_DLL main.cpp -ID:\gcc\boost\include\boost-1_69 -LD:\gcc\boost\lib -lwsock32 -lws2_32 -lboost_system-mgw81-mt-x64-1_69 -o print.dll

g++ -static -Wall -shared -g -DBUILD_DLL main.cpp -ID:\gcc\boost\include\boost-1_69 -LD:\gcc\boost\lib -lwsock32 -lws2_32 -lboost_system-mgw81-mt-x64-1_69

加一句免第三方dll

以下源码编译成功

#ifndef __MAIN_H__
#define __MAIN_H__ #ifndef VC_EXTRALEAN
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#endif // Modify the following defines if you have to target a platform prior to the ones specified below.
// Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef WINVER // Allow use of features specific to Windows 95 and Windows NT 4 or later.
#define WINVER 0x0500 // Change this to the appropriate value to target Windows 98 and Windows 2000 or later.
#endif #ifndef _WIN32_WINNT // Allow use of features specific to Windows NT 4 or later.
#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target Windows 2000 or later.
#endif #ifndef _WIN32_WINDOWS // Allow use of features specific to Windows 98 or later.
#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later.
#endif #ifndef _WIN32_IE // Allow use of features specific to IE 4.0 or later.
#define _WIN32_IE 0x0400 // Change this to the appropriate value to target IE 5.0 or later.
#endif #define WIN32_LEAN_AND_MEAN // 在#include<windows.h>前定义
#include <windows.h> /* To use this exported function of dll, include this header
* in your project.
*/ #ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif #ifdef __cplusplus
extern "C"
{
#endif DLL_EXPORT int Asio_init(int iport); DLL_EXPORT int Asio_SvrRun(); DLL_EXPORT int Asio_Uninit(int iport); DLL_EXPORT int Asio_SetCallback(int ikind,int ifun); DLL_EXPORT int Asio_senddata(int ikind,int* isocket,char * ibuff,int ilen); DLL_EXPORT int Asio_closesocket(int* isocket); DLL_EXPORT int Asio_Client_init(int* ipobj); DLL_EXPORT int Asio_Client_conntosvr(int* isocket,char* iip,int iport,int iuserdata); DLL_EXPORT int Asio_Client_DisConn(int* isocket); DLL_EXPORT int Asio_Client_Free(int* isocket); DLL_EXPORT int Asio_Client_senddata(int* isocket,char * ibuff,int ilen); DLL_EXPORT int Asio_Client_readdata(int* isocket,char * ibuff,int ilen); DLL_EXPORT int Asio_Client_Asreaddata(int* isocket,char * ibuff,int ilen,int iuserdata); #ifdef __cplusplus
}
#endif #endif // __MAIN_H__
#define DLL _stdcall

#include "main.h"

#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <windows.h>
#include <List> using boost::asio::ip::tcp; bool iscreate=false;
bool isfinished=false;
int gport;
class session;
class server;
boost::asio::io_service io_service;
boost::asio::deadline_timer* lt;
server* s; std::list<session*> deadlist;
std::list<session*>::iterator deadlist_Iter,deadlist_Iter2;
CRITICAL_SECTION criCounter; //回调函数设置
typedef void (CALLBACK* Cb_OnNewConn)(int ipsocket,const char* ipeerip,int ipeerport,int* iuserdata,int* ireadlen);
typedef void (CALLBACK* Cb_OnDisConn)(int iuserdata);
typedef void (CALLBACK* Cb_Onreaddata)(char* ibuff,int ilen,int iuserdata,int* ireadlen);
typedef void (CALLBACK* Cb_Onwritedata)(int iuserdata,int iuser2); Cb_OnNewConn Callback_NewConn;
Cb_OnDisConn Callback_DisConn;
Cb_Onreaddata Callback_readdata;
Cb_Onwritedata Callback_writedata; class session
{
public:
int readlen;
int userdata;
int deadtime;
enum { max_length = };
char data_[max_length]; session(boost::asio::io_service& io_service)
: socket_(io_service)
{
//data_[0]='a';
//data_[1]='b';
//data_[2]='1';
readlen=;
userdata=;
}
~session()
{
socket_.close();
// delete socket_;
} tcp::socket& socket()
{
return socket_;
} void start()
{
freopen("start.txt", "w+", stdout);
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
} void handle_read(const boost::system::error_code& error,
size_t bytes_transferred)
{
freopen("handle_read.txt", "w+", stdout);
// std::cout << "read some len:" << bytes_transferred << "\n" ;
if (!error)
{ try
{
if(Callback_readdata!=NULL)
{
Callback_readdata(data_,bytes_transferred, userdata,&readlen); }
}
catch(...)
{
} if(userdata!=)
{
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
socket_.close();
if(Callback_DisConn!=NULL)
{
Callback_DisConn(userdata);
}
delete this;
}
}
else
{ socket_.close();
// cout << "socket error " << error << "\n" ;
if(Callback_DisConn!=NULL)
{
Callback_DisConn(userdata);
} delete this;
}
} void handle_write(const boost::system::error_code& error,int iuser,int iuser2)
{
freopen("handle_write.txt", "w+", stdout);
if(Callback_writedata!=NULL)
{
// std::cout << "write ok " << iuser << "\n" ;
Callback_writedata(iuser,iuser2);
}
if (!error)
{ }
else
{
socket_.close();
if(Callback_DisConn!=NULL)
{
Callback_DisConn(userdata);
} delete this;
}
} private:
tcp::socket socket_; }; class server
{
public:
server(boost::asio::io_service& io_service, short port)
: io_service_(io_service),
acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
{
freopen("server.txt", "w+", stdout);
//创建临界区
InitializeCriticalSection(&criCounter); session* new_session = new session(io_service_); acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error
));
}
~server()
{
//删除临界区
DeleteCriticalSection(&criCounter); }
void handle_accept(session* new_session,
const boost::system::error_code& error
)
{
int lbuf;
// cout << "some one conned ok \n" ;
if (!error)
{
if(Callback_NewConn!=NULL)
{ lbuf=(int &)(*new_session);
// std::cout <<"socket addr:"<<lbuf << "\n" ;
// cout <<"IP:"<<new_session->socket().remote_endpoint().address().to_string()<< " port:"<<new_session->socket().remote_endpoint().port()<<" \n" ;
/* std::cout <<"userdata:"<<&new_session->userdata<< "\n" ;
std::cout <<"readlen:"<<&new_session->readlen<< "\n" ; */ Callback_NewConn(lbuf,new_session->socket().remote_endpoint().address().to_string().c_str(),
new_session->socket().remote_endpoint().port(),
&new_session->userdata,
&new_session->readlen); new_session->start();
new_session = new session(io_service_);
// //删除超时的sesson
// int i;
// for (deadlist_Iter=deadlist.begin(); deadlist_Iter !=deadlist.end(); ++deadlist_Iter)
//{
// i= (*deadlist_Iter)->deadtime;
// i=GetTickCount() -i;
//
// if ( i > 3000)
// {
// //用pos2来保存pos指针
// deadlist_Iter2=deadlist_Iter;
// deadlist_Iter++;
// deadlist.erase(deadlist_Iter2);
// delete (*deadlist_Iter);
// }
//} }
acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error));
/* session* lp=(session*)(lbuf);
boost::asio::async_write(lp->socket() ,
boost::asio::buffer(new_session->data_, 4),
boost::bind(&session::handle_write,
new_session,
boost::asio::placeholders::error,
1
));*/ }
else
{
delete new_session;
}
} private: boost::asio::io_service& io_service_;
tcp::acceptor acceptor_;
}; void ontimer(const boost::system::error_code& /*e*/)
{
freopen("ontimer.txt", "w+", stdout);
if(isfinished==false)
{
lt->expires_from_now(boost::posix_time::seconds());
lt->async_wait(boost::bind(ontimer,boost::asio::placeholders::error));
}
} //typedef int (CALLBACK* fPlayerlst)(char* ibuff,int i);
//typedef int (CALLBACK* frecordlst)(char* ibuff,int i); extern __declspec(dllexport) int Asio_init(int iport)
{
freopen("Asio_init.txt", "w+", stdout);
if(iscreate==false)
{
/* AllocConsole();
freopen( "CONOUT$", "w", stdout );
freopen( "CONOUT$", "w", stderr );
freopen( "CONIN$", "r", stdin );
cout << "init \n" ;*/
gport=iport;
iscreate=true;
if(iport==)
//server s(io_service, iport);
//else
{
lt=new boost::asio::deadline_timer(io_service,boost::posix_time::seconds());
lt->expires_from_now(boost::posix_time::seconds());
lt->async_wait(boost::bind(ontimer,boost::asio::placeholders::error));
}
return ;
}
return ;
//
//if (access("plog", 0) == 0)
//{ //}
} extern __declspec(dllexport) int Asio_SvrRun()
{
freopen("Asio_SvrRun.txt", "w+", stdout);
if(gport!=)
{
s=new server(io_service,gport);
}
try
{
io_service.run();
}
catch(...)
{
}
delete s; if(gport==)
{
Sleep();
delete lt;
} return ;
} extern __declspec(dllexport) int Asio_Uninit(int iport)
{
freopen("Asio_Uninit.txt", "w+", stdout);
if(gport==)
{
lt->cancel();
}
io_service.stop();
isfinished=true;
return ;
} extern __declspec(dllexport) int Asio_SetCallback(int ikind,int ifun)
{
freopen("Asio_SetCallback.txt", "w+", stdout);
if(ikind==)
Callback_NewConn=(Cb_OnNewConn)ifun;
else if(ikind==)
Callback_DisConn=(Cb_OnDisConn)ifun;
else if(ikind==)
Callback_readdata=(Cb_Onreaddata)ifun;
else if(ikind==)
Callback_writedata=(Cb_Onwritedata)ifun;
return ;
} extern __declspec(dllexport) int Asio_senddata(int ikind,int* isocket,char * ibuff,int ilen)
{
freopen("Asio_senddata.txt", "w+", stdout);
session* ltmp=(session*)isocket; boost::asio::async_write(ltmp->socket(),
boost::asio::buffer(ibuff, ilen),
boost::bind(&session::handle_write,
ltmp,
boost::asio::placeholders::error,
ltmp->userdata,
ikind
));
return ;
} extern __declspec(dllexport) int Asio_closesocket(int* isocket)
{
freopen("Asio_closesocket.txt", "w+", stdout);
//isocket->userdata=0;//不允许再触发
session* lsock=(session*)isocket;
try
{
lsock->deadtime=GetTickCount();
lsock->socket().cancel();
boost::system::error_code ignored_ec;
lsock->socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
lsock->socket().close(); }
catch(...)
{}
//EnterCriticalSection(&criCounter);
//deadlist.push_back(isocket);
//LeaveCriticalSection(&criCounter); //delete isocket;
return ;
} extern __declspec(dllexport) int Asio_Client_init(int* ipobj)
{
freopen("Asio_Client_init.txt", "w+", stdout);
session* new_session;
new_session=new session(io_service);
*ipobj=(int &)(*new_session);
return ;
} extern __declspec(dllexport) int Asio_Client_conntosvr(int* isocket,char* iip,int iport,int iuserdata)
{
freopen("Asio_Client_conntosvr.txt", "w+", stdout);
//域名解析,只将域名解析为ipv4地址
boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address_v4::from_string(iip), iport);
//只连接一次
boost::system::error_code ec;
session * lsock=(session *)isocket;
lsock->userdata=iuserdata;
lsock->socket().connect(ep,ec);
if (ec)
return ;
else
{
lsock->socket().async_read_some(boost::asio::buffer(lsock->data_, lsock->max_length),
boost::bind(&session::handle_read, lsock,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
return ;
} } extern __declspec(dllexport) int Asio_Client_DisConn(int * isocket)
{
freopen("Asio_Client_DisConn.txt", "w+", stdout);
//isocket->userdata=0;//不允许再触发
session* lsock=(session*)isocket;
try
{
lsock->deadtime=GetTickCount();
lsock->socket().cancel();
boost::system::error_code ignored_ec;
lsock->socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
lsock->socket().close();
}
catch(...)
{
}
return ;
} extern __declspec(dllexport) int Asio_Client_Free(int * isocket)
{
freopen("Asio_Client_Free.txt", "w+", stdout);
delete (session*)isocket;
return ;
} extern __declspec(dllexport) int Asio_Client_senddata(int * isocket,char * ibuff,int ilen)
{
freopen("Asio_Client_senddata.txt", "w+", stdout);
session* lsock=(session*)isocket;
boost::system::error_code ignored_error;
boost::asio::write(lsock->socket(), boost::asio::buffer(ibuff,ilen),
boost::asio::transfer_all(), ignored_error);
if(ignored_error)
return ;
else
return ;
} extern __declspec(dllexport) int Asio_Client_readdata(int * isocket,char * ibuff,int ilen)
{
freopen("Asio_Client_readdata.txt", "w+", stdout);
boost::system::error_code ignored_error;
session* lsock=(session*)isocket;
ilen=lsock->socket().read_some(boost::asio::buffer(ibuff,ilen), ignored_error);
if(ignored_error)
return ;
else
return ilen;
} extern __declspec(dllexport) int Asio_Client_Asreaddata(int * isocket,char * ibuff,int ilen,int iuserdata)
{
freopen("Asio_Client_Asreaddata.txt", "w+", stdout); session * lsock=(session *)isocket; lsock->userdata=iuserdata;
lsock->socket().async_read_some(boost::asio::buffer(lsock->data_, lsock->max_length),
boost::bind(&session::handle_read, lsock,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
return ;
}

MSYS2 MinGW 64-bit

./b2 install --prefix=/usr

./b2 -a cxxflags="-DVC_EXTRALEAN -DWINVER=0x0400 -D_WIN32_WINNT=0x0501 -D_WIN32_WINDOWS=0x0410 -D_WIN32_IE=0x0400"  toolset=gcc --这条不安装费事

./b2 -a cxxflags="-DVC_EXTRALEAN -DWINVER=0x0400 -D_WIN32_WINNT=0x0501 -D_WIN32_WINDOWS=0x0410 -D_WIN32_IE=0x0400"  toolset=gcc install --prefix=(D:\GCC\boost) (/usr)

./bootstrap.sh

#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <list>
#ifdef WIN32
#include "windows.h"
#include <List>
#endif using boost::asio::ip::tcp; bool iscreate=false;
bool isfinished=false;
int gport;
class session;
class server;
boost::asio::io_service io_service;
boost::asio::deadline_timer* lt;
server* s; std::list<session*> deadlist;
std::list<session*>::iterator deadlist_Iter,deadlist_Iter2;
//CRITICAL_SECTION criCounter; //回调函数设置
typedef void (*Cb_OnNewConn)(int ipsocket,const char* ipeerip,int ipeerport,int* iuserdata,int* ireadlen);
typedef void (*Cb_OnDisConn)(int iuserdata);
typedef void (*Cb_Onreaddata)(char* ibuff,int ilen,int iuserdata,int* ireadlen);
typedef void (*Cb_Onwritedata)(int iuserdata,int iuser2); Cb_OnNewConn Callback_NewConn;
Cb_OnDisConn Callback_DisConn;
Cb_Onreaddata Callback_readdata;
Cb_Onwritedata Callback_writedata; class session
{
public:
int readlen;
int userdata;
int deadtime;
enum { max_length = };
char data_[max_length]; session(boost::asio::io_service& io_service)
: socket_(io_service)
{
//data_[0]='a';
//data_[1]='b';
//data_[2]='1';
readlen=;
userdata=;
}
~session()
{
socket_.close();
// delete socket_;
} tcp::socket& socket()
{
return socket_;
} void start()
{
freopen("start.txt", "w+", stdout);
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
} void handle_read(const boost::system::error_code& error,
size_t bytes_transferred)
{
freopen("handle_read.txt", "w+", stdout);
// std::cout << "read some len:" << bytes_transferred << "\n" ;
if (!error)
{ try
{
if(Callback_readdata!=NULL)
{
Callback_readdata(data_,bytes_transferred, userdata,&readlen); }
}
catch(...)
{
} if(userdata!=)
{
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
socket_.close();
if(Callback_DisConn!=NULL)
{
Callback_DisConn(userdata);
}
delete this;
}
}
else
{ socket_.close();
// cout << "socket error " << error << "\n" ;
if(Callback_DisConn!=NULL)
{
Callback_DisConn(userdata);
} delete this;
}
} void handle_write(const boost::system::error_code& error,int iuser,int iuser2)
{
freopen("handle_write.txt", "w+", stdout);
if(Callback_writedata!=NULL)
{
// std::cout << "write ok " << iuser << "\n" ;
Callback_writedata(iuser,iuser2);
}
if (!error)
{ }
else
{
socket_.close();
if(Callback_DisConn!=NULL)
{
Callback_DisConn(userdata);
} delete this;
}
} private:
tcp::socket socket_; }; class server
{
public:
server(boost::asio::io_service& io_service, short port)
: io_service_(io_service),
acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
{
freopen("server.txt", "w+", stdout);
//创建临界区
// InitializeCriticalSection(&criCounter); session* new_session = new session(io_service_); acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error
));
}
~server()
{
//删除临界区
// DeleteCriticalSection(&criCounter); }
void handle_accept(session* new_session,
const boost::system::error_code& error
)
{
int lbuf;
// cout << "some one conned ok \n" ;
if (!error)
{
if(Callback_NewConn!=NULL)
{ lbuf=(int &)(*new_session);
// std::cout <<"socket addr:"<<lbuf << "\n" ;
// cout <<"IP:"<<new_session->socket().remote_endpoint().address().to_string()<< " port:"<<new_session->socket().remote_endpoint().port()<<" \n" ;
/* std::cout <<"userdata:"<<&new_session->userdata<< "\n" ;
std::cout <<"readlen:"<<&new_session->readlen<< "\n" ; */ Callback_NewConn(lbuf,new_session->socket().remote_endpoint().address().to_string().c_str(),
new_session->socket().remote_endpoint().port(),
&new_session->userdata,
&new_session->readlen); new_session->start();
new_session = new session(io_service_);
// //删除超时的sesson
// int i;
// for (deadlist_Iter=deadlist.begin(); deadlist_Iter !=deadlist.end(); ++deadlist_Iter)
//{
// i= (*deadlist_Iter)->deadtime;
// i=GetTickCount() -i;
//
// if ( i > 3000)
// {
// //用pos2来保存pos指针
// deadlist_Iter2=deadlist_Iter;
// deadlist_Iter++;
// deadlist.erase(deadlist_Iter2);
// delete (*deadlist_Iter);
// }
//} }
acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error));
/* session* lp=(session*)(lbuf);
boost::asio::async_write(lp->socket() ,
boost::asio::buffer(new_session->data_, 4),
boost::bind(&session::handle_write,
new_session,
boost::asio::placeholders::error,
));*/ }
else
{
delete new_session;
}
} private: boost::asio::io_service& io_service_;
tcp::acceptor acceptor_;
}; void ontimer(const boost::system::error_code& /*e*/)
{
freopen("ontimer.txt", "w+", stdout);
if(isfinished==false)
{
lt->expires_from_now(boost::posix_time::seconds());
lt->async_wait(boost::bind(ontimer,boost::asio::placeholders::error));
}
} unsigned long GetTickCount()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC,&ts);
return (ts.tv_sec * + ts.tv_nsec / );
} //typedef int (CALLBACK* fPlayerlst)(char* ibuff,int i);
//typedef int (CALLBACK* frecordlst)(char* ibuff,int i); int Asio_init(int iport)
{
freopen("Asio_init.txt", "w+", stdout);
if(iscreate==false)
{
/* AllocConsole();
freopen( "CONOUT$", "w", stdout );
freopen( "CONOUT$", "w", stderr );
freopen( "CONIN$", "r", stdin );
cout << "init \n" ;*/
gport=iport;
iscreate=true;
if(iport==)
//server s(io_service, iport);
//else
{
lt=new boost::asio::deadline_timer(io_service,boost::posix_time::seconds());
lt->expires_from_now(boost::posix_time::seconds());
lt->async_wait(boost::bind(ontimer,boost::asio::placeholders::error));
}
return ;
}
return ;
//
//if (access("plog", 0) == 0)
//{ //}
} int Asio_SvrRun()
{
freopen("Asio_SvrRun.txt", "w+", stdout);
if(gport!=)
{
s=new server(io_service,gport);
}
try
{
io_service.run();
}
catch(...)
{
}
delete s; if(gport==)
{
sleep();
delete lt;
} return ;
} int Asio_Uninit(int iport)
{
freopen("Asio_Uninit.txt", "w+", stdout);
if(gport==)
{
lt->cancel();
}
io_service.stop();
isfinished=true;
return ;
} int Asio_SetCallback(int ikind,long long ifun)
{
freopen("Asio_SetCallback.txt", "w+", stdout);
if(ikind==)
Callback_NewConn=(Cb_OnNewConn)ifun;
else if(ikind==)
Callback_DisConn=(Cb_OnDisConn)ifun;
else if(ikind==)
Callback_readdata=(Cb_Onreaddata)ifun;
else if(ikind==)
Callback_writedata=(Cb_Onwritedata)ifun;
return ;
} int Asio_senddata(int ikind,int* isocket,char * ibuff,int ilen)
{
freopen("Asio_senddata.txt", "w+", stdout);
session* ltmp=(session*)isocket; boost::asio::async_write(ltmp->socket(),
boost::asio::buffer(ibuff, ilen),
boost::bind(&session::handle_write,
ltmp,
boost::asio::placeholders::error,
ltmp->userdata,
ikind
));
return ;
} int Asio_closesocket(int* isocket)
{
freopen("Asio_closesocket.txt", "w+", stdout);
//isocket->userdata=0;//不允许再触发
session* lsock=(session*)isocket;
try
{
lsock->deadtime=GetTickCount();
lsock->socket().cancel();
boost::system::error_code ignored_ec;
lsock->socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
lsock->socket().close(); }
catch(...)
{}
//EnterCriticalSection(&criCounter);
//deadlist.push_back(isocket);
//LeaveCriticalSection(&criCounter); //delete isocket;
return ;
} int Asio_Client_init(int* ipobj)
{
freopen("Asio_Client_init.txt", "w+", stdout);
session* new_session;
new_session=new session(io_service);
*ipobj=(int &)(*new_session);
return ;
} int Asio_Client_conntosvr(int* isocket,char* iip,int iport,int iuserdata)
{
freopen("Asio_Client_conntosvr.txt", "w+", stdout);
//域名解析,只将域名解析为ipv4地址
boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address_v4::from_string(iip), iport);
//只连接一次
boost::system::error_code ec;
session * lsock=(session *)isocket;
lsock->userdata=iuserdata;
lsock->socket().connect(ep,ec);
if (ec)
return ;
else
{
lsock->socket().async_read_some(boost::asio::buffer(lsock->data_, lsock->max_length),
boost::bind(&session::handle_read, lsock,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
return ;
} } int Asio_Client_DisConn(int * isocket)
{
freopen("Asio_Client_DisConn.txt", "w+", stdout);
//isocket->userdata=0;//不允许再触发
session* lsock=(session*)isocket;
try
{
lsock->deadtime=GetTickCount();
lsock->socket().cancel();
boost::system::error_code ignored_ec;
lsock->socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
lsock->socket().close();
}
catch(...)
{
}
return ;
} int Asio_Client_Free(int * isocket)
{
freopen("Asio_Client_Free.txt", "w+", stdout);
delete (session*)isocket;
return ;
} int Asio_Client_senddata(int * isocket,char * ibuff,int ilen)
{
freopen("Asio_Client_senddata.txt", "w+", stdout);
session* lsock=(session*)isocket;
boost::system::error_code ignored_error;
boost::asio::write(lsock->socket(), boost::asio::buffer(ibuff,ilen),
boost::asio::transfer_all(), ignored_error);
if(ignored_error)
return ;
else
return ;
} int Asio_Client_readdata(int * isocket,char * ibuff,int ilen)
{
freopen("Asio_Client_readdata.txt", "w+", stdout);
boost::system::error_code ignored_error;
session* lsock=(session*)isocket;
ilen=lsock->socket().read_some(boost::asio::buffer(ibuff,ilen), ignored_error);
if(ignored_error)
return ;
else
return ilen;
} int Asio_Client_Asreaddata(int * isocket,char * ibuff,int ilen,int iuserdata)
{
freopen("Asio_Client_Asreaddata.txt", "w+", stdout); session * lsock=(session *)isocket; lsock->userdata=iuserdata;
lsock->socket().async_read_some(boost::asio::buffer(lsock->data_, lsock->max_length),
boost::bind(&session::handle_read, lsock,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
return ;
}

gcc main.cpp -Wall -g -DBUILD_DLL -DWIN32_LEAN_AND_MEAN -o main -user32 -l:ws2_32.dll -lboost_system -m32

gcc -LC:\Windows\System32 main.cpp ws2_32.dll

gcc -LC:\Windows\System32 -Wall -d'__USE_W32_SOCKETS' -g -DBUILD_DLL -DWIN32_LEAN_AND_MEAN main.cpp -D_WIN32_WINNT=0x0501 -D__USE_W32_SOCKETS -luser32 -user32 -l:ws2_32.dll -lboost_system -m32
-D_WIN32_WINNT=0x0501 -D__USE_W32_SOCKETS -std=c++11 -lboost_system -lws2_32

gcc -Wall -g -DBUILD_DLL -DWIN32_LEAN_AND_MEAN main.cpp -DVC_EXTRALEAN -DWINVER=0x0400 -D_WIN32_WINNT=0x0501 -D_WIN32_WINDOWS=0x0410 -D_WIN32_IE=0x0400 -D__USE_W32_SOCKETS -luser32 -user32 -lws2_32 -lboost_system -m32

4.2、完全安装boost库

执行下面命令即可

./bootstrap --buildtype=complete install
1
这样可以安装所有的测试版,发行版,静态库和动态库。

4.3、定制安装

用户可以根据自己的选择来编译需要使用的库

下面这条命令是查看所有必须编译才能使用的库

./b2 --show-libraries
1
在完全编译的基础上,使用–with或者–without选项可打开或者关闭某个库的编译,如

./b2 --with-date_time --buildtype=complete install
1
将仅仅编译安装date_time库

---------------------
作者:郭世江
来源:CSDN
原文:https://blog.csdn.net/jiang_xinxing/article/details/60150346
版权声明:本文为博主原创文章,转载请附上博文链接!

下面是测试boost是否安装成功的代码

#include <boost/version.hpp>
#include <boost/config.hpp>
int main()

{
cout << BOOST_VERSION <<endl;
cout << BOOST_LIB_VERSION<<endl;
cout << BOOST_STDLIB <<endl;
cout <<BOOST_PLATFORM <<endl;
cout << BOOST_COMPILER<<endl;
}

---------------------
作者:郭世江
来源:CSDN
原文:https://blog.csdn.net/jiang_xinxing/article/details/60150346
版权声明:本文为博主原创文章,转载请附上博文链接!

#define DLL _stdcall

#include "main.h"

#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <windows.h>
#include <List> using boost::asio::ip::tcp; bool iscreate=false;
bool isfinished=false;
int gport;
class session;
class server;
boost::asio::io_service io_service;
boost::asio::deadline_timer* lt;
server* s; std::list<session*> deadlist;
std::list<session*>::iterator deadlist_Iter,deadlist_Iter2;
CRITICAL_SECTION criCounter; //回调函数设置
typedef void (CALLBACK* Cb_OnNewConn)(int ipsocket,const char* ipeerip,int ipeerport,int* iuserdata,int* ireadlen);
typedef void (CALLBACK* Cb_OnDisConn)(int iuserdata);
typedef void (CALLBACK* Cb_Onreaddata)(char* ibuff,int ilen,int iuserdata,int* ireadlen);
typedef void (CALLBACK* Cb_Onwritedata)(int iuserdata,int iuser2); Cb_OnNewConn Callback_NewConn;
Cb_OnDisConn Callback_DisConn;
Cb_Onreaddata Callback_readdata;
Cb_Onwritedata Callback_writedata; class session
{
public:
int readlen;
int userdata;
int deadtime;
enum { max_length = };
char data_[max_length]; session(boost::asio::io_service& io_service)
: socket_(io_service)
{
//data_[0]='a';
//data_[1]='b';
//data_[2]='1';
readlen=;
userdata=;
}
~session()
{
socket_.close();
// delete socket_;
} tcp::socket& socket()
{
return socket_;
} void start()
{
freopen("start.txt", "w+", stdout);
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
} void handle_read(const boost::system::error_code& error,
size_t bytes_transferred)
{
freopen("handle_read.txt", "w+", stdout);
// std::cout << "read some len:" << bytes_transferred << "\n" ;
if (!error)
{ try
{
if(Callback_readdata!=NULL)
{
Callback_readdata(data_,bytes_transferred, userdata,&readlen); }
}
catch(...)
{
} if(userdata!=)
{
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
socket_.close();
if(Callback_DisConn!=NULL)
{
Callback_DisConn(userdata);
}
delete this;
}
}
else
{ socket_.close();
// cout << "socket error " << error << "\n" ;
if(Callback_DisConn!=NULL)
{
Callback_DisConn(userdata);
} delete this;
}
} void handle_write(const boost::system::error_code& error,int iuser,int iuser2)
{
freopen("handle_write.txt", "w+", stdout);
if(Callback_writedata!=NULL)
{
// std::cout << "write ok " << iuser << "\n" ;
Callback_writedata(iuser,iuser2);
}
if (!error)
{ }
else
{
socket_.close();
if(Callback_DisConn!=NULL)
{
Callback_DisConn(userdata);
} delete this;
}
} private:
tcp::socket socket_; }; class server
{
public:
server(boost::asio::io_service& io_service, short port)
: io_service_(io_service),
acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
{
freopen("server.txt", "w+", stdout);
//创建临界区
InitializeCriticalSection(&criCounter); session* new_session = new session(io_service_); acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error
));
}
~server()
{
//删除临界区
DeleteCriticalSection(&criCounter); }
void handle_accept(session* new_session,
const boost::system::error_code& error
)
{
int lbuf;
// cout << "some one conned ok \n" ;
if (!error)
{
if(Callback_NewConn!=NULL)
{ lbuf=(int &)(*new_session);
// std::cout <<"socket addr:"<<lbuf << "\n" ;
// cout <<"IP:"<<new_session->socket().remote_endpoint().address().to_string()<< " port:"<<new_session->socket().remote_endpoint().port()<<" \n" ;
/* std::cout <<"userdata:"<<&new_session->userdata<< "\n" ;
std::cout <<"readlen:"<<&new_session->readlen<< "\n" ; */ Callback_NewConn(lbuf,new_session->socket().remote_endpoint().address().to_string().c_str(),
new_session->socket().remote_endpoint().port(),
&new_session->userdata,
&new_session->readlen); new_session->start();
new_session = new session(io_service_);
// //删除超时的sesson
// int i;
// for (deadlist_Iter=deadlist.begin(); deadlist_Iter !=deadlist.end(); ++deadlist_Iter)
//{
// i= (*deadlist_Iter)->deadtime;
// i=GetTickCount() -i;
//
// if ( i > 3000)
// {
// //用pos2来保存pos指针
// deadlist_Iter2=deadlist_Iter;
// deadlist_Iter++;
// deadlist.erase(deadlist_Iter2);
// delete (*deadlist_Iter);
// }
//} }
acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error));
/* session* lp=(session*)(lbuf);
boost::asio::async_write(lp->socket() ,
boost::asio::buffer(new_session->data_, 4),
boost::bind(&session::handle_write,
new_session,
boost::asio::placeholders::error,
1
));*/ }
else
{
delete new_session;
}
} private: boost::asio::io_service& io_service_;
tcp::acceptor acceptor_;
}; void ontimer(const boost::system::error_code& /*e*/)
{
freopen("ontimer.txt", "w+", stdout);
if(isfinished==false)
{
lt->expires_from_now(boost::posix_time::seconds());
lt->async_wait(boost::bind(ontimer,boost::asio::placeholders::error));
}
} //typedef int (CALLBACK* fPlayerlst)(char* ibuff,int i);
//typedef int (CALLBACK* frecordlst)(char* ibuff,int i); extern __declspec(dllexport) int Asio_init(int iport)
{
freopen("Asio_init.txt", "w+", stdout);
if(iscreate==false)
{
/* AllocConsole();
freopen( "CONOUT$", "w", stdout );
freopen( "CONOUT$", "w", stderr );
freopen( "CONIN$", "r", stdin );
cout << "init \n" ;*/
gport=iport;
iscreate=true;
if(iport==)
//server s(io_service, iport);
//else
{
lt=new boost::asio::deadline_timer(io_service,boost::posix_time::seconds());
lt->expires_from_now(boost::posix_time::seconds());
lt->async_wait(boost::bind(ontimer,boost::asio::placeholders::error));
}
return ;
}
return ;
//
//if (access("plog", 0) == 0)
//{ //}
} extern __declspec(dllexport) int Asio_SvrRun()
{
freopen("Asio_SvrRun.txt", "w+", stdout);
if(gport!=)
{
s=new server(io_service,gport);
}
try
{
io_service.run();
}
catch(...)
{
}
delete s; if(gport==)
{
Sleep();
delete lt;
} return ;
} extern __declspec(dllexport) int Asio_Uninit(int iport)
{
freopen("Asio_Uninit.txt", "w+", stdout);
if(gport==)
{
lt->cancel();
}
io_service.stop();
isfinished=true;
return ;
} extern __declspec(dllexport) int Asio_SetCallback(int ikind,int ifun)
{
freopen("Asio_SetCallback.txt", "w+", stdout);
if(ikind==)
Callback_NewConn=(Cb_OnNewConn)ifun;
else if(ikind==)
Callback_DisConn=(Cb_OnDisConn)ifun;
else if(ikind==)
Callback_readdata=(Cb_Onreaddata)ifun;
else if(ikind==)
Callback_writedata=(Cb_Onwritedata)ifun;
return ;
} extern __declspec(dllexport) int Asio_senddata(int ikind,int* isocket,char * ibuff,int ilen)
{
freopen("Asio_senddata.txt", "w+", stdout);
session* ltmp=(session*)isocket; boost::asio::async_write(ltmp->socket(),
boost::asio::buffer(ibuff, ilen),
boost::bind(&session::handle_write,
ltmp,
boost::asio::placeholders::error,
ltmp->userdata,
ikind
));
return ;
} extern __declspec(dllexport) int Asio_closesocket(int* isocket)
{
freopen("Asio_closesocket.txt", "w+", stdout);
//isocket->userdata=0;//不允许再触发
session* lsock=(session*)isocket;
try
{
lsock->deadtime=GetTickCount();
lsock->socket().cancel();
boost::system::error_code ignored_ec;
lsock->socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
lsock->socket().close(); }
catch(...)
{}
//EnterCriticalSection(&criCounter);
//deadlist.push_back(isocket);
//LeaveCriticalSection(&criCounter); //delete isocket;
return ;
} extern __declspec(dllexport) int Asio_Client_init(int* ipobj)
{
freopen("Asio_Client_init.txt", "w+", stdout);
session* new_session;
new_session=new session(io_service);
*ipobj=(int &)(*new_session);
return ;
} extern __declspec(dllexport) int Asio_Client_conntosvr(int* isocket,char* iip,int iport,int iuserdata)
{
freopen("Asio_Client_conntosvr.txt", "w+", stdout);
//域名解析,只将域名解析为ipv4地址
boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address_v4::from_string(iip), iport);
//只连接一次
boost::system::error_code ec;
session * lsock=(session *)isocket;
lsock->userdata=iuserdata;
lsock->socket().connect(ep,ec);
if (ec)
return ;
else
{
lsock->socket().async_read_some(boost::asio::buffer(lsock->data_, lsock->max_length),
boost::bind(&session::handle_read, lsock,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
return ;
} } extern __declspec(dllexport) int Asio_Client_DisConn(int * isocket)
{
freopen("Asio_Client_DisConn.txt", "w+", stdout);
//isocket->userdata=0;//不允许再触发
session* lsock=(session*)isocket;
try
{
lsock->deadtime=GetTickCount();
lsock->socket().cancel();
boost::system::error_code ignored_ec;
lsock->socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
lsock->socket().close();
}
catch(...)
{
}
return ;
} extern __declspec(dllexport) int Asio_Client_Free(int * isocket)
{
freopen("Asio_Client_Free.txt", "w+", stdout);
delete (session*)isocket;
return ;
} extern __declspec(dllexport) int Asio_Client_senddata(int * isocket,char * ibuff,int ilen)
{
freopen("Asio_Client_senddata.txt", "w+", stdout);
session* lsock=(session*)isocket;
boost::system::error_code ignored_error;
boost::asio::write(lsock->socket(), boost::asio::buffer(ibuff,ilen),
boost::asio::transfer_all(), ignored_error);
if(ignored_error)
return ;
else
return ;
} extern __declspec(dllexport) int Asio_Client_readdata(int * isocket,char * ibuff,int ilen)
{
freopen("Asio_Client_readdata.txt", "w+", stdout);
boost::system::error_code ignored_error;
session* lsock=(session*)isocket;
ilen=lsock->socket().read_some(boost::asio::buffer(ibuff,ilen), ignored_error);
if(ignored_error)
return ;
else
return ilen;
} extern __declspec(dllexport) int Asio_Client_Asreaddata(int * isocket,char * ibuff,int ilen,int iuserdata)
{
freopen("Asio_Client_Asreaddata.txt", "w+", stdout); session * lsock=(session *)isocket; lsock->userdata=iuserdata;
lsock->socket().async_read_some(boost::asio::buffer(lsock->data_, lsock->max_length),
boost::bind(&session::handle_read, lsock,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
return ;
} BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
freopen("DllMain.txt", "w+", stdout);
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break; case DLL_PROCESS_DETACH:
// detach from process
break; case DLL_THREAD_ATTACH:
// attach to thread
break; case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}
#ifndef __MAIN_H__
#define __MAIN_H__ #include <windows.h> /* To use this exported function of dll, include this header
* in your project.
*/ #ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif #ifdef __cplusplus
extern "C"
{
#endif DLL_EXPORT int Asio_init(int iport); DLL_EXPORT int Asio_SvrRun(); DLL_EXPORT int Asio_Uninit(int iport); DLL_EXPORT int Asio_SetCallback(int ikind,int ifun); DLL_EXPORT int Asio_senddata(int ikind,int* isocket,char * ibuff,int ilen); DLL_EXPORT int Asio_closesocket(int* isocket); DLL_EXPORT int Asio_Client_init(int* ipobj); DLL_EXPORT int Asio_Client_conntosvr(int* isocket,char* iip,int iport,int iuserdata); DLL_EXPORT int Asio_Client_DisConn(int* isocket); DLL_EXPORT int Asio_Client_Free(int* isocket); DLL_EXPORT int Asio_Client_senddata(int* isocket,char * ibuff,int ilen); DLL_EXPORT int Asio_Client_readdata(int* isocket,char * ibuff,int ilen); DLL_EXPORT int Asio_Client_Asreaddata(int* isocket,char * ibuff,int ilen,int iuserdata); #ifdef __cplusplus
}
#endif #endif // __MAIN_H__
上一篇:Linux C++ 开发简介(包括Linux守护线程)


下一篇:boost中全局命名锁的使用