I'm trying to create a library that uses Boost ASIO (UDP multicast, asynchronous) and does not expose it. Basically I am following the async udp example, except I have made the io_service object a static private member of the library.
The code works fine if directly compiled into an executable. If I try and make a static library and then use it inside an application, the code throws exceptions while constructing my class.
If anyone has examples or has created a library that uses Boost ASIO and does not expose it and could comment I'd greatly appreciate the help.
I've also tried making io_service a private member of the class and also tried passing it to the constructor. Everything I've tried so far has thrown exceptions.
Here is the example code for the library:
#ifdef _LIB
static boost::asio::io_service asio_service;
#endif
class udpframereader
{
public:
udpframereader() : m_socket(asio_service)
{
m_packetCount = 0;
...
}
unsigned long long asio_error_count();
...
#ifdef _LIB
private:
void handle_receive(const boost::system::error_code& error, std::size_t bytes_transferred);
boost::asio::ip::udp::socket m_socket;
boost::asio::ip::udp::endpoint m_remote_endpoint;
boost::array<char, 4096> m_buffer;
boost::crc_ccitt_type m_crc;
unsigned long long m_packetCount;
...
#endif
};
ifdef _LIBs? - bdonlanifdef _LIBallows me to use the same .h file inside the library and the application that uses the library._LIBis not defined when used by the application. Also, the constructor is really in a separate cpp file, but I've copy pasted it in just to show how it looks. - Mark_LIBlike you have. In C++, the class definition ofudpframereaderin the library must be identical to the one that the library's user sees. Among other reasons, C++ must allocate the same-sized data structure for each object ofudpframereader. - chrisaycock