Boost Asio serial port issue
I'm using CodeBlocks on a windows systems and have downloaded Boost, complied and setup my IDE variables and build options. I have been successfully using other boost libraries and now I need to work on a program that reads and writes to a serial port.
I can not get any example that I try to compile for asio serial port. The following, for example will generate a compile error which follows the code:
#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;
}
This is the build log for the above code:
Compiling: main.cpp
In fi开发者_StackOverflowle 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:
Any advice ??
From the boost/asio/serial_port_base.hpp file (a bit simplified):
#if defined(BOOST_ASIO_HAS_IOCP) || !defined(BOOST_WINDOWS)
# define BOOST_ASIO_HAS_SERIAL_PORT 1
#endif
So BOOST_ASIO_HAS_SERIAL_PORT is true in Windows only if BOOST_ASIO_HAS_IOCP is also true.
Then, from 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
So, if I'm following it right, you need to define _WIN32_WINNT to 0x0400 or above to enable it.
精彩评论