fails with this error:

1>c:\source\<snip>\boost\1.51.0\boost\asio\detail\socket_types.hpp(22): fatal error c1189: #error :  winsock.h has already been included

on the other hand

#include <boost/asio.hpp>
#include <windows.h>

produces a bunch of noise and sets the windows version # incorrectly

1?  please define _win32_winnt or _win32_windows appropriately. for example:
1>  - add -d_win32_winnt=0x0501 to the compiler command line; or
1>  - add _win32_winnt=0x0501 to your project's preprocessor definitions.
1>  assuming _win32_winnt=0x0501 (i.e. windows xp target).

i couldn't find any way around this that didn't leave a bad taste, but this:

#ifdef _win32
#  ifdef use_asio
//     set the proper sdk version before including boost/asio
#      include <sdkddkver.h>
//     note boost/asio includes windows.h. 
#      include <boost/asio.hpp>
#   else //  use_asio
#      include <windows.h>
#   endif //  use_asio
#else // _win32
#  ifdef use_asio
#     include <boost/asio.hpp>
#  endif // use_asio
#endif //_win32

does produce a clean compile.

<editorial> it shouldn't be that hard </editorial>

#if _win32_winnt <= 0x0501 #define boost_asio_disable_iocp #define boost_asio_enable_cancelio #endif #endif

an other workarround i used is to concentrate all asio dependent code in an xxx.hpp file and include it on the top of each windows implementing xxx.cpp file where you use its objects.

this method place the include asio above any other include windows.h and work arround the problem.

for me, switching the order of includes caused compile errors with another microsoft include i was using - that was declaring things with "typedef interface".

since my error was coming from socket_types.h, from these lines:

# if defined(_winsockapi_) && !defined(_winsock2api_)
#  error winsock.h has already been included
# endif // defined(_winsockapi_) && !defined(_winsock2api_)

i put an include of "winsock2.h" before the windows.h, and then finally the boost/asio.hpp include, and things then compiled happily.

try and change the order of includes. start with boost/asio.hpp and put windows.h after it.

usually the writers of any code library solve the compatibility issues but they can do it better if their code is the first to meet the compiler and preprocessor.

there's a similar issue with ace, including ace/os.h before anything else solves it.

  • Boost::asio winsock and winsock 2 compatibility issue
  • "Launch Failed. Binary Not Found." Snow Leopard and Eclipse C/C++ IDE issue
  • Compatibility of *.dll *.a *.lib *.def between VisualStudio and gcc
  • Link compatibility between C++ and D
  • _GLIBCXX_USE_CXX11_ABI, GCC 4.8 and ABI compatibility
  • winsock compiling error, it cant find the addrinfo structures and some relating functions
  • Is catastrophic cancellation an issue when calculating dot products of floating point vectors? And if so, how is it typically addressed?
  • Integration issue to tess-two (Tesseract Tools for Android)library into an Android studio and build ndk
  • Issue with std::shared_ptr, inheritance, and template argument deduction
  • Non-obvious lifetime issue with std::promise and std::future
  • Java and C++ on Stack Unwinding issue
  • Library compatibility between C++11 and C++03
  • Extending a class and maintaining binary backward compatibility
  • How do I debug or fix the endless loop and heap corruption issue involving boost::interprocess managed_shared_memory?
  • a small issue with std::vector and changing the collection while looping through it
  • Issue with enable_if and multiple conditions
  • OpenGL Core and Compatibility
  • Pure virtual functions and binary compatibility
  • multiple definitions error in c++ and solution to solve this issue
  • Build issue with MSVS 2010 and the C++ standard
  • Maintaining code compatibility between OpenCV 2 and OpenCV 3
  • Bluetooth with C++ and winsock
  • Boost and ssl client server building issue on Linux
  • Mixed C++ and Fortran Linking Issue
  • Direct2D interface and blurry text issue
  • Inheriting a class and re-using its constructors issue missing in the base class
  • Binary files and cross platform compatibility
  • extern variable and array declare issue c++
  • Issue with Freetype and OpenGL
  • g++ -std=c++0x and compatibility
  • More Query from same tag

  • Does C++ derived class ever have to include definitions of inherited functions/members in header file?
  • C++ pointer names
  • How to use circularly dependent classes in C++?
  • operator "<<" to a pointer
  • Casting a C++ long type to a JNI jlong
  • A compile time way to determine the least expensive argument type
  • Best approach to define a constant (used in a constant expression) in the class?
  • OpenCV draw rectangles around only large contours?
  • How to handle "item not found" situations in a find function?
  • Calling private method in C++
  • Is erasing element after before_begin() defined in an empty std::forward_list?
  • Trouble with Polygon in Stroustrup's PPP book
  • Hide a file or directory using the Windows API from C
  • Trying to strip project path from __FILE__
  • Error building Qt app for Android with Qt Creator
  • Why does long long n = 2000*2000*2000*2000; overflow?
  • Why might std::vector be faster than a raw dynamically allocated array?
  • Constructor conditionally marked explicit
  • datetime insertion in SQLite database using QT and parameters
  • Compiled Qt for windows is win api at Low Level
  • Linux C++: how to profile time wasted due to cache misses?
  • How can I audit my Windows application for correct Unicode handling?
  • Change Text Color in MFC C++?
  • Treat vector<int*> as vector<const int*> without copying (C++0x)
  • best practice when returning smart pointers
  • C++ method parameter passed by reference - memory question
  • Templates accepting "anything" in C++
  • Deallocation doesn't free memory in Windows/C++ Application
  • Linux C/C++ Timer signal handler in userspace
  • Why is there no std::on_exit?
  •