【问题标题】:Boost Asio serial port issueBoost Asio 串口问题
【发布时间】:2011-08-16 16:55:49
【问题描述】:

我在 Windows 系统上使用 CodeBlocks,并下载了 Boost,编译并设置了我的 IDE 变量和构建选项。我已经成功地使用了其他 boost 库,现在我需要编写一个可以读写串行端口的程序。

我无法获得任何我尝试为 asio 串行端口编译的示例。例如,以下代码将生成一个编译错误:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/serial_port.hpp>
#include <boost/thread.hpp>



int main()
{


    boost::asio::io_service io_service;
    boost::asio::serial_port port(io_service);


    return 0;

}

这是上述代码的构建日志:

Compiling: main.cpp
In file included from C:\Dev\boost_1_47_0/boost/thread/win32/thread_data.hpp:12,
                 from C:\Dev\boost_1_47_0/boost/thread/thread.hpp:15,
                 from C:\Dev\boost_1_47_0/boost/thread.hpp:13,
                 from C:\Users\bjune\Documents\CodeBlocks\hellow\main.cpp:4:
C:\Dev\boost_1_47_0/boost/thread/win32/thread_heap_alloc.hpp:59: warning: inline function 'void* boost::detail::allocate_raw_heap_memory(unsigned int)' declared as  dllimport: attribute ignored
C:\Dev\boost_1_47_0/boost/thread/win32/thread_heap_alloc.hpp:69: warning: inline function 'void boost::detail::free_raw_heap_memory(void*)' declared as  dllimport: attribute ignored
C:\Users\bjune\Documents\CodeBlocks\hellow\main.cpp: In function 'int main()':
C:\Users\bjune\Documents\CodeBlocks\hellow\main.cpp:13: error: 'serial_port' is not a member of 'boost::asio'
C:\Users\bjune\Documents\CodeBlocks\hellow\main.cpp:13: error: expected ';' before 'port'
C:\Dev\boost_1_47_0/boost/system/error_code.hpp: At global scope:

有什么建议吗??

【问题讨论】:

    标签: c++ windows boost serial-port boost-asio


    【解决方案1】:

    来自 boost/asio/serial_port_base.hpp 文件(有点简化):

    #if defined(BOOST_ASIO_HAS_IOCP) || !defined(BOOST_WINDOWS)
    #  define BOOST_ASIO_HAS_SERIAL_PORT 1
    #endif
    

    因此,只有当 BOOST_ASIO_HAS_IOCP 也为真时,BOOST_ASIO_HAS_SERIAL_PORT 在 Windows 中才为真。

    然后,从 boost/asio/detail/win_iocp_io_service_fwd.hpp

    #if defined(BOOST_WINDOWS)
    #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0400)
    
    // Define this to indicate that IOCP is supported on the target platform.
    #  define BOOST_ASIO_HAS_IOCP 1
    #endif
    #endif
    

    所以,如果我没听错的话,你需要将 _WIN32_WINNT 定义为 0x0400 或更高才能启用它。

    【讨论】:

      猜你喜欢
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 2019-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多