Code :: Blocks和Boost Asio。它不会工作

时间:2012-07-16 01:35:32

标签: c++ windows boost-asio codeblocks

我无法在Code :: Blocks中编译Boost.Asio。我安装并编译了Boost C ++库,并运行了给定的测试示例。现在我想让Boost.Asio运行。

这是编译器调用:

mingw32-g++.exe -L"C:\Program Files (x86)\boost\boost_1_49_0" -o bin\Debug\server.exe obj\Debug\server.o D:\CodeBlocksIDE\CodeBlocks\lib\libboost_filesystem-mgw44-mt-1_49.a D:\CodeBlocksIDE\CodeBlocks\lib\libboost_system-mgw44-mt-1_49.a

以下是输出(有关详情,请参阅this链接):

boost/asio/detail/impl/winsock_init.ipp:39: undefined reference to 'WSAStartup@8'
boost/asio/detail/impl/winsock_init.ipp:48: undefined reference to 'WSAStartup@0'
boost/asio/detail/impl/socket_ops.ipps:50: undefined reference to 'WSASetLastError@4'
boost/asio/detail/impl/socket_ops.ipps:178: undefined reference to 'GetAcceptExSockaddrs@32'
boost/asio/detail/impl/socket_ops.ipps:296: undefined reference to 'closesocket@4'

以下是代码:

//
// server.cpp
// ~~~~~~~~~~
//
// Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <cstdlib>
#include <iostream>
#include <boost/aligned_storage.hpp>
#include <boost/array.hpp>
#include <boost/bind.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

// Class to manage the memory to be used for handler-based custom allocation.
// It contains a single block of memory which may be returned for allocation
// requests. If the memory is in use when an allocation request is made, the
// allocator delegates allocation to the global heap.
class handler_allocator
  : private boost::noncopyable
{
public:
  handler_allocator()
    : in_use_(false)
  {
  }

  void* allocate(std::size_t size)
  {
    if (!in_use_ && size < storage_.size)
    {
      in_use_ = true;
      return storage_.address();
    }
    else
    {
      return ::operator new(size);
    }
  }

  void deallocate(void* pointer)
  {
    if (pointer == storage_.address())
    {
      in_use_ = false;
    }
    else
    {
      ::operator delete(pointer);
    }
  }

private:
  // Storage space used for handler-based custom memory allocation.
  boost::aligned_storage<1024> storage_;

  // Whether the handler-based custom allocation storage has been used.
  bool in_use_;
};

// Wrapper class template for handler objects to allow handler memory
// allocation to be customised. Calls to operator() are forwarded to the
// encapsulated handler.
template <typename Handler>
class custom_alloc_handler
{
public:
  custom_alloc_handler(handler_allocator& a, Handler h)
    : allocator_(a),
      handler_(h)
  {
  }

  template <typename Arg1>
  void operator()(Arg1 arg1)
  {
    handler_(arg1);
  }

  template <typename Arg1, typename Arg2>
  void operator()(Arg1 arg1, Arg2 arg2)
  {
    handler_(arg1, arg2);
  }

  friend void* asio_handler_allocate(std::size_t size,
      custom_alloc_handler<Handler>* this_handler)
  {
    return this_handler->allocator_.allocate(size);
  }

  friend void asio_handler_deallocate(void* pointer, std::size_t /*size*/,
      custom_alloc_handler<Handler>* this_handler)
  {
    this_handler->allocator_.deallocate(pointer);
  }

private:
  handler_allocator& allocator_;
  Handler handler_;
};

// Helper function to wrap a handler object to add custom allocation.
template <typename Handler>
inline custom_alloc_handler<Handler> make_custom_alloc_handler(
    handler_allocator& a, Handler h)
{
  return custom_alloc_handler<Handler>(a, h);
}

class session
  : public boost::enable_shared_from_this<session>
{
public:
  session(boost::asio::io_service& io_service)
    : socket_(io_service)
  {
  }

  tcp::socket& socket()
  {
    return socket_;
  }

  void start()
  {
    socket_.async_read_some(boost::asio::buffer(data_),
        make_custom_alloc_handler(allocator_,
          boost::bind(&session::handle_read,
            shared_from_this(),
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred)));
  }

  void handle_read(const boost::system::error_code& error,
      size_t bytes_transferred)
  {
    if (!error)
    {
      boost::asio::async_write(socket_,
          boost::asio::buffer(data_, bytes_transferred),
          make_custom_alloc_handler(allocator_,
            boost::bind(&session::handle_write,
              shared_from_this(),
              boost::asio::placeholders::error)));
    }
  }

  void handle_write(const boost::system::error_code& error)
  {
    if (!error)
    {
      socket_.async_read_some(boost::asio::buffer(data_),
          make_custom_alloc_handler(allocator_,
            boost::bind(&session::handle_read,
              shared_from_this(),
              boost::asio::placeholders::error,
              boost::asio::placeholders::bytes_transferred)));
    }
  }

private:
  // The socket used to communicate with the client.
  tcp::socket socket_;

  // Buffer used to store data received from the client.
  boost::array<char, 1024> data_;

  // The allocator to use for handler-based custom memory allocation.
  handler_allocator allocator_;
};

typedef boost::shared_ptr<session> session_ptr;

class server
{
public:
  server(boost::asio::io_service& io_service, short port)
    : io_service_(io_service),
      acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
  {
    session_ptr new_session(new session(io_service_));
    acceptor_.async_accept(new_session->socket(),
        boost::bind(&server::handle_accept, this, new_session,
          boost::asio::placeholders::error));
  }

  void handle_accept(session_ptr new_session,
      const boost::system::error_code& error)
  {
    if (!error)
    {
      new_session->start();
    }

    new_session.reset(new session(io_service_));
    acceptor_.async_accept(new_session->socket(),
        boost::bind(&server::handle_accept, this, new_session,
          boost::asio::placeholders::error));
  }

private:
  boost::asio::io_service& io_service_;
  tcp::acceptor acceptor_;
};

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 2)
    {
      std::cerr << "Usage: server <port>\n";
      return 1;
    }

    boost::asio::io_service io_service;

    using namespace std; // For atoi.
    server s(io_service, atoi(argv[1]));

    io_service.run();
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}

以下是一些关于我的问题的图片。

http://imageshack.us/f/267/cdbideboost1.png

http://imageshack.us/f/833/cdbideboost3.png

http://imageshack.us/f/31/cdbideboost4.png

http://imageshack.us/f/525/cdbideboost5.png

2 个答案:

答案 0 :(得分:11)

链接到库wsock32以及ws2_32

enter image description here

答案 1 :(得分:2)

链接器错误消息表明您需要链接到WinSock库。 Boost.Asio是一个头文件库;因此,没有boost_asio库可以链接。


以下示例程序旨在介绍WinSock库的依赖项。

example.cpp

#include <iostream>
#include <winsock2.h>

int main()
{
  WSAStartup(WORD(), LPWSADATA());
  WSACleanup();
  return 0;
}

编译example.cpp但未链接到WinSock库时,会出现以下错误:

C:\example>mingw32-g++.exe example.cpp
example.cpp:(.text+0x26): undefined reference to `WSAStartup@8'
example.cpp:(.text+0x2e): undefined reference to `WSACleanup@0'
collect2: ld returned 1 exit status

但是,当我链接到ws2_32时,引用已解决:

C:\example>mingw32-g++.exe example.cpp -lws2_32

C:\example>dir /B
a.exe
example.cpp

另请注意,指定文件的顺序很重要。必须在所有相关模块的名称后指定库。在这种情况下,example.cpp取决于ws2_32;因此,ws2_32必须出现在example.cpp之后。

C:\example>mingw32-g++.exe -lws2_32 example.cpp
example.cpp:(.text+0x26): undefined reference to `WSAStartup@8'
example.cpp:(.text+0x2e): undefined reference to `WSACleanup@0'
collect2: ld returned 1 exit status