栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

VScode CMake 编写 Boost Asio 程序----记录5

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

VScode CMake 编写 Boost Asio 程序----记录5

1. 调试Asio 异步时,出现错误:
D:/boost_1_79_0/boost/asio/detail/impl/win_iocp_socket_service_base.ipp:570: undefined reference to `AcceptEx'
2. 原因是缺少库

mswsock

3. 解决办法: 修改cmakefile如下:
cmake_minimum_required(VERSION 3.1...23.0)
project(server VERSION 0.1.0)
add_definitions(-std=c++17)
include(CTest)
enable_testing()

#Boost
set(BOOST_ROOT "D:\boost_1_79_0")
set(BOOST_INCLUDEDIR "D:\boost_1_79_0")
set(BOOST_LIBRARYDIR "D:\boost_1_79_0\stage\lib")
set(Boost_DEBUG ON)
set(Boost_NO_BOOST_CMAKE ON)

set(Boost_LIB_PREFIX "lib")
set(Boost_ARCHITECTURE "-x64") 
 
#set(Boost_USE_STATIC_LIBS ON)
find_package(Boost 1.79.0 REQUIRED COMPONENTS thread )

if (Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    MESSAGE( STATUS "-------------------------------------------")
    MESSAGE( STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}.")
    MESSAGE( STATUS "Boost_LIBRARY_DIRS = ${Boost_LIBRARY_DIRS}.")
    MESSAGE( STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}.")
    MESSAGE( STATUS "Boost_LIB_VERSION = ${Boost_LIB_VERSION}.")
    MESSAGE( STATUS "-------------------------------------------")

    add_executable(server server.cpp)
    
    target_link_libraries(server ws2_32 mswsock  ${Boost_LIBRARIES})
    #target_link_libraries(server  ws2_32  )
endif()

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

 4. 测试文件:

#include 
#include 
#include 
#include 
#include 
#include 
#include 

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

std::string make_daytime_string()
{
  using namespace std; // For time_t, time and ctime;
  time_t now = time(0);
  return ctime(&now);
}

class tcp_connection
  : public boost::enable_shared_from_this
{
public:
  typedef boost::shared_ptr pointer;

  static pointer create(boost::asio::io_context& io_context)
  {
    return pointer(new tcp_connection(io_context));
  }

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

  void start()
  {
    message_ = make_daytime_string();

    boost::asio::async_write(socket_, boost::asio::buffer(message_),
        boost::bind(&tcp_connection::handle_write, shared_from_this(),
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
  }

private:
  tcp_connection(boost::asio::io_context& io_context)
    : socket_(io_context)
  {
  }

  void handle_write(const boost::system::error_code& ,
      size_t )
  {
  }

  tcp::socket socket_;
  std::string message_;
};

class tcp_server
{
public:
  tcp_server(boost::asio::io_context& io_context)
    : io_context_(io_context),
      acceptor_(io_context, tcp::endpoint(tcp::v4(), 13))
  {
    start_accept();
  }

private:
  void start_accept()
  {
    tcp_connection::pointer new_connection =
      tcp_connection::create(io_context_);

    acceptor_.async_accept(new_connection->socket(),
        boost::bind(&tcp_server::handle_accept, this, new_connection,
          boost::asio::placeholders::error));
  }

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

    start_accept();
  }

  boost::asio::io_context& io_context_;
  tcp::acceptor acceptor_;
};

int main()
{
  try
  {
    boost::asio::io_context io_context;
    tcp_server server(io_context);
    io_context.run();
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/879510.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号