Boost::asio winsock and winsock 2 compatibility issue

后端 未结 5 553
难免孤独
难免孤独 2020-12-28 16:57

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

相关标签:
5条回答
  • 2020-12-28 17:07
    #ifdef BOOST_OS_WINDOWS
    #define _WIN32_WINNT 0x0501
    #if _WIN32_WINNT <= 0x0501
    #define BOOST_ASIO_DISABLE_IOCP
    #define BOOST_ASIO_ENABLE_CANCELIO
    #endif
    #endif
    
    0 讨论(0)
  • 2020-12-28 17:11

    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.

    0 讨论(0)
  • 2020-12-28 17:13

    As Danius (the OP) points out a compile with

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

    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>

    0 讨论(0)
  • 2020-12-28 17:17

    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.

    0 讨论(0)
  • 2020-12-28 17:21

    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.

    0 讨论(0)
提交回复
热议问题