C++ Boost::asio winsock 和 winsock 2 兼容性问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9750344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Boost::asio winsock and winsock 2 compatibility issue
提问by Dainius Kreivys
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.
我的项目使用 windows.h,其中使用了 winsock.h,我需要包含使用 winsock2 的 boost:assio。所以我收到很多错误,说 Winsock.h 已经包含在内。我可以定义 WIN32_LEAN_AND_MEAN。这样 windows.h 就不会使用 winsock。问题是,我需要 windows.h 才能使用它,而我只需要 Asio 用于异步计时器。我不需要它的 winsock2.h 。我尝试搜索如何禁用它的 winsock2 使用,我发现我可以通过在包含 boost:asio 之前定义 BOOST_ASIO_NO_WIN32_LEAN_AND_MEAN 来做到这一点,但我仍然遇到相同的错误。
#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
1>c:\program files\boost\boost_1_47\boost\asio\detail\socket_types.hpp(22): fatal error C1189: #error : WinSock.h 已经被包含
回答by selalerer
Try and change the order or includes. Start with boost/asio.hpp and put windows.h after it.
尝试更改顺序或包含。从 boost/asio.hpp 开始,然后将 windows.h 放在它之后。
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.
ACE 也有类似的问题,包括 ace/OS.h,然后其他任何方法都解决了它。
回答by Theanderblast
For me, switching the order of includes caused compile errors with another Microsoft include I was using - that was declaring things with "typedef interface".
对我来说,切换包含的顺序会导致我使用的另一个 Microsoft 包含导致编译错误 - 使用“typedef 接口”声明内容。
Since my error was coming from socket_types.h, from these lines:
由于我的错误来自 socket_types.h,来自以下几行:
# 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.
我在 Windows.h 之前放了一个“winsock2.h”的包含,最后是 boost/asio.hpp 包含,然后事情就愉快地编译了。
回答by Dale Wilson
As Danius (the OP) points out a compile with
正如 Danius(OP)指出的编译
#include <windows.h>
#include <boost/asio.hpp>
fails with this error:
失败并出现此错误:
1>c:\source\<SNIP>\boost.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
产生一堆噪音并错误地设置了 windows 版本 #
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>
<EDITORIAL>应该没那么难</EDITORIAL>
回答by William H Myatt
#ifdef BOOST_OS_WINDOWS
#define _WIN32_WINNT 0x0501
#if _WIN32_WINNT <= 0x0501
#define BOOST_ASIO_DISABLE_IOCP
#define BOOST_ASIO_ENABLE_CANCELIO
#endif
#endif
回答by ori
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.
我使用的另一个解决方法是将所有与 asio 相关的代码集中在 XXX.hpp 文件中,并将其包含在每个实现 XXX.cpp 文件的窗口的顶部,您可以在其中使用其对象。
this method place the include asio above any other include windows.h and work arround the problem.
此方法将 include asio 置于任何其他 include windows.h 之上并解决问题。