使用 windows.h 和 WIN32_LEAN_AND_MEAN 时未定义 timeval
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1909358/
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
timeval undefined when using windows.h and WIN32_LEAN_AND_MEAN
提问by tjd
To avoid conflicts with winsock2.h, I want to wrap my include of windows.h with WIN32_LEAN_AND_MEAN (I undef it after windows.h so as not to interfere with applications that include my headers). Doing this causes timeval to be undefined when winsock2.h isn't included. Including time.h doesn't define timeval either.
为了避免与 winsock2.h 发生冲突,我想用 WIN32_LEAN_AND_MEAN 包装 windows.h 的包含(我在 windows.h 之后取消定义它,以免干扰包含我的标题的应用程序)。当不包含 winsock2.h 时,这样做会导致 timeval 未定义。包括 time.h 也不定义 timeval。
How can I get timeval defined (a) without having to include winsock2.h, (b) not requiring applications that include my headers to include winsock2.h before my headers, (c) allowing application to include winsock2.h if they need them, and (d) not having to define timeval myself, because it may already be defined by a header the parent application is including?
如何定义 timeval (a) 而不必包含 winsock2.h,(b) 不需要包含我的头文件的应用程序在我的头文件之前包含 winsock2.h,(c) 允许应用程序在需要时包含 winsock2.h , 和 (d) 不必自己定义 timeval,因为它可能已经由父应用程序包含的标头定义?
回答by Robert Buckingham
I don't define either of the LEAN_AND_MEANs, instead I explicitly do the following for each of the 'NOs' prior to including windows.h
我没有定义 LEAN_AND_MEAN 中的任何一个,而是在包含 windows.h 之前为每个“NO”明确执行以下操作
// Exclude Input Method Manager (International stuff...one day)
#if !defined NOIMM
#define NOIME
#define NOIMM
#endif
// Exclude Metafile API
#if !defined NOMETAFILE
#define NOMETAFILE
#endif
This allows me to use the same StdAfx.h. With VS2010 I can include Winsock2 after windows.h without conflicts.
这允许我使用相同的 StdAfx.h。使用 VS2010,我可以在 windows.h 之后包含 Winsock2 而不会发生冲突。
Stdafx is a little longer, but clearly documents what is included and excluded.
Stdafx 有点长,但清楚地记录了包含和排除的内容。
回答by Martyn Lovell
Remember that WIN32_LEAN_AND_MEAN is a compiler performance optimisation. Using it makes your apps compile somewhat faster at the expense of omitting some less-used parts of the Windows API.
请记住, WIN32_LEAN_AND_MEAN 是编译器性能优化。使用它可以让你的应用程序编译得更快一些,但代价是省略了 Windows API 中一些较少使用的部分。
You could use one of the more granular disables (NOIMM, etc), but these have even less impact on compile time than the LEAN_AND_MEAN one does.
您可以使用更精细的禁用之一(NOIMM 等),但与 LEAN_AND_MEAN 相比,这些对编译时间的影响甚至更小。
Unless your project is very large and has long, onerous compile times I would simply stop using WIN32_LEAN_AND_MEAN.
除非您的项目非常大并且编译时间长且繁重,否则我将停止使用 WIN32_LEAN_AND_MEAN。
Martyn
马丁
回答by Aaron Klotz
This is how I handle problems with winsock2.h
(assuming Visual C++):
这就是我处理问题的方式winsock2.h
(假设是 Visual C++):
- I configured the build system to always pass in
/D_WINSOCKAPI_
to the compiler on the command line so that no winsock header files are ever implicitly included. - When I want to include
winsock2.h
, I do it via a proxy header file that does some preprocessor magic for me.
- 我将构建系统配置为始终在
/D_WINSOCKAPI_
命令行上传递给编译器,这样就不会隐式包含 winsock 头文件。 - 当我想包含时
winsock2.h
,我通过一个代理头文件来完成,该文件为我做了一些预处理器魔术。
This is how my header file works:
这是我的头文件的工作方式:
#pragma push_macro("_WINSOCKAPI_")
#undef _WINSOCKAPI_
#include <winsock2.h>
#pragma pop_macro("_WINSOCKAPI_")
If you do this then you don't need to #define WIN32_LEAN_AND_MEAN
and you can use winsock2.h
only when needed.
如果你这样做,那么你就不需要#define WIN32_LEAN_AND_MEAN
,你可以winsock2.h
只在需要时使用。
回答by Luke
I don't think what you want is possible. The timeval struct is defined in winsock.h and winsock2.h, thus you have to include one of those to get the definition. If you don't define WIN32_LEAN_AND_MEAN, windows.h will include winsock.h; this makes it impossible to include winsock2.h. If you define WIN32_LEAN_AND_MEAN, neither of the winsock header files are automatically included thus you don't get the definition for timeval. I think your best option is to require applications to include one of the winsock headers explicitly.
我不认为你想要的是可能的。timeval 结构在 winsock.h 和 winsock2.h 中定义,因此您必须包含其中之一才能获得定义。如果你没有定义 WIN32_LEAN_AND_MEAN,windows.h 会包含 winsock.h;这使得无法包含 winsock2.h。如果您定义 WIN32_LEAN_AND_MEAN,则不会自动包含两个 winsock 头文件,因此您不会获得 timeval 的定义。我认为您最好的选择是要求应用程序明确包含 winsock 标头之一。