boost使用常见问题集锦

常见问题

windows 环境下出现如下:
Invoking: MinGW C++ Linker
g++ -std=c++11 -mwindows  ./src/GMTP/GMPacket.o  ./src/main.o    -L/mingw64/lib -LC:/building/msys64/mingw64/lib -L/mingw64/lib -LC:/building/msys64/mingw64/lib/../lib -L/mingw64/lib -LC:/building/msys64/mingw64/lib -L/mingw64/lib -lgtkmm-3.0 -latkmm-1.6 -lgdkmm-3.0 -lgiomm-2.4 -lpangomm-1.4 -lglibmm-2.4 -lgtk-3 -lgdk-3 -lgdi32 -limm32 -lshell32 -lole32 -Wl,-luuid -lwinmm -ldwmapi -lsetupapi -lcfgmgr32 -lz -lepoxy -lpangocairo-1.0 -lpangoft2-1.0 -lpangowin32-1.0 -lgdi32 -lusp10 -lpango-1.0 -lm -latk-1.0 -lcairo-gobject -lgio-2.0 -lz -lcairomm-1.0 -lcairo -lz -lpixman-1 -lfontconfig -lexpat -lfreetype -liconv -lexpat -lfreetype -lz -lbz2 -lharfbuzz -lgraphite2 -lpng16 -lz -lsigc-2.0 -lgdk_pixbuf-2.0 -lgmodule-2.0 -pthread -lpng16 -lz -lgobject-2.0 -lffi -lglib-2.0 -lintl -pthread -lws2_32 -lole32 -lwinmm -lshlwapi -lpcre -lintl -lpcre  -lboost_system-mt -o "gateway.exe"
./src/main.o: In function `boost::asio::detail::socket_ops::complete_iocp_accept(unsigned long long, void*, unsigned long, sockaddr*, unsigned long long*, unsigned long long, boost::system::error_code&)':
E:/2017/msys64/mingw64/include/boost/asio/detail/impl/socket_ops.ipp:199: undefined reference to `GetAcceptExSockaddrs'
./src/main.o: In function `boost::asio::detail::win_iocp_socket_service_base::start_accept_op(boost::asio::detail::win_iocp_socket_service_base::base_implementation_type&, bool, boost::asio::detail::socket_holder&, int, int, int, void*, unsigned long, boost::asio::detail::win_iocp_operation*)':
E:/2017/msys64/mingw64/include/boost/asio/detail/impl/win_iocp_socket_service_base.ipp:495: undefined reference to `AcceptEx'
./src/main.o: In function `boost::asio::detail::win_iocp_socket_service_base::restart_accept_op(unsigned long long, boost::asio::detail::socket_holder&, int, int, int, void*, unsigned long, boost::asio::detail::win_iocp_operation*)':
E:/2017/msys64/mingw64/include/boost/asio/detail/impl/win_iocp_socket_service_base.ipp:520: undefined reference to `AcceptEx'
collect2.exe: error: ld returned 1 exit status
make: *** [makefile:48: gateway.exe] Error 1

问题解决:在如下图位置添加依赖库-lwsock32

boost使用常见问题集锦_第1张图片
image.png

Mac 下出现如下问题:
../src/main.cpp:22:18: error: no member named 'bind' in namespace 'std'
            std::bind(&Server::accept_handler, this, std::placeholders::_1, sock));
            ~~~~~^
../src/main.cpp:31:18: error: no member named 'bind' in namespace 'std'
            std::bind(&Server::write_handler, this, std::placeholders::_1));
            ~~~~~^
2 warnings and 2 errors generated.
make: *** [src/main.o] Error 1

问题解决:指定c++编译版本即可解决
参考如下图设置:如下两张图位置都需要设置

boost使用常见问题集锦_第2张图片
image.png
boost使用常见问题集锦_第3张图片
image.png

服务端代码:

#include 
#include 
using namespace boost::asio;

class Server
{
    typedef ip::tcp::socket socket_type;
    typedef std::shared_ptr sock_ptr;
public:
    Server() :m_acceptor(m_io, ip::tcp::endpoint(ip::tcp::v4(), 6688))
    {
        accept();
    }
    void run()
    {
        m_io.run();
    }
private:
    void accept()
    {
        sock_ptr sock(new socket_type(m_io));
        m_acceptor.async_accept(*sock,
        [this, sock](const boost::system::error_code& ec)
        {
            if (ec)
            {
                return;
            }
            std::cout << "client:";
            std::cout << sock->remote_endpoint().address() << std::endl;
            sock->async_write_some(buffer("hello asio"),
                [](const boost::system::error_code&, std::size_t)
                {
                    std::cout << "send msg complete." << std::endl;
                });
            accept();
        });
    }
private:
    io_service m_io;
    ip::tcp::acceptor m_acceptor;
};

int main()
{
    try
    {
        std::cout << "server" << std::endl;
        Server svr;
        svr.run();
    }
    catch (std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }
    return 0;
}

客户端代码

#include 
#include 
using namespace boost::asio;

class Client
{
    typedef ip::tcp::socket socket_type;
    typedef std::shared_ptr sock_ptr;

public:

    Client() :m_ep(ip::address::from_string("192.168.0.104"), 6688)
    {
        start();
    }

    void run()
    {
        m_io.run();
    }

private:
    void start()
    {
        sock_ptr sock(new socket_type(m_io));
        sock->async_connect(m_ep, std::bind(&Client::connect_handler, this, std::placeholders::_1, sock));
    }

    void connect_handler(const boost::system::error_code& ec, sock_ptr sock)
    {
        if (ec)
        {return;}
        std::cout << "receive from:" << sock->remote_endpoint().address() << std::endl;
        sock->async_read_some(buffer(m_buf),
            std::bind(&Client::read_handler, this, std::placeholders::_1));
    }

    void read_handler(const boost::system::error_code& ec)
    {
        if (ec)
        {return;}
        std::cout << m_buf << std::endl;
    }

private:
    io_service m_io;
    ip::tcp::endpoint m_ep;

    enum {max_length = 1024 };
    char m_buf[max_length];
};

int main()
{
    try
    {
        std::cout << "client start." << std::endl;
        Client cl;
        cl.run();
    }
    catch (std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }
    getchar();
    return 0;
}

你可能感兴趣的:(boost使用常见问题集锦)