Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

My project uses windows.h in which winsock.h is used, and I need to include boost:assio which uses winsock2. So I get many errors that says Winsock.h already included. I can define WIN32_LEAN_AND_MEAN. so that windows.h wouldn't use winsock. The problem is , that I need windows.h to use it, and I just need Asio for asynchronous timers. I don't need its winsock2.h . I tried searching how to disable its winsock2 use, and I found that I could do that by defining BOOST_ASIO_NO_WIN32_LEAN_AND_MEAN before including boost:asio, but I still get the same error.

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

Error

1>c:\program files\boost\boost_1_47\boost\asio\detail\socket_types.hpp(22): fatal error C1189: #error : WinSock.h has already been included

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.

One more question: Can an application read packets with winsock2 from another application witch sends packets with winsock1 ? And vice verse. – Dainius Kreivys Mar 18, 2012 at 18:51 @DainiusKreivys I'm not sure what are the updates that were done from winsock1 to 2. I can only guess it was mainly bug fixes. Bugs aside everything should work ok, but since there probably are bugs, they might come up. – selalerer Mar 19, 2012 at 8:40 Changing the order of includes didn't really fix the problem, just changed it from an error to a warning. – Dale Wilson Apr 29, 2013 at 21:54

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>

Put this in a header file where you might otherwise include asio.hpp and windows.h and #define USE_ASIO in a cpp file that includes the header file. – Dale Wilson Jun 15, 2018 at 14:46 #include <SDKDDKVer.h> was the missing idea that finally helped me overcome all this. thank you! – pestophagous Mar 16, 2021 at 2:02

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.

#if _WIN32_WINNT <= 0x0501 #define BOOST_ASIO_DISABLE_IOCP #define BOOST_ASIO_ENABLE_CANCELIO #endif #endif It's normaly considered good form to explain your suggestions/answers. Code only answers can be surprisingly uninformative, even if they are technically correct. – Maximilian Ast Nov 16, 2016 at 7:13

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.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.