C++ mysql and boost asio header conflict
-
10-07-2019 - |
Question
There seems to be a conflict with the windows headers between the mysql c-api and boost::asio.
If I include mysql first I get:
boost/asio/detail/socket_types.hpp(27) : fatal error C1189: #error : WinSock.h has already been included
#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
# if defined(_WINSOCKAPI_) && !defined(_WINSOCK2API_)
# error WinSock.h has already been included
# endif // defined(_WINSOCKAPI_) && !defined(_WINSOCK2API_)
If I include boost::asio first I get:
include\config-win.h(24) : warning C4005: '_WIN32_WINNT' : macro redefinition
/* Defines for Win32 to make it compatible for MySQL */
#ifdef __WIN2000__
/* We have to do this define before including windows.h to get the AWE API
functions */
#define _WIN32_WINNT 0x0500
#else
/* Get NT 4.0 functions */
#define _WIN32_WINNT 0x0400
#endif
Is there some way around this, and why is mysql trying to force the windows version and boost trying to enforce that it include winsock its self anyway?
Solution
The macro redefinition is only a warning. Your code should still compile and link. I think your code will even work without any problem.
OTHER TIPS
Try
#include "winsock2.h"
before including mysql and boost::asio
If you can't find a way to get around this problem, you could try wrapping the MySQL API behind an opaque pointer as a last resort.