使用 windows.h 和 WIN32_LEAN_AND_MEAN 时 timeval 未定义
为了避免与winsock2.h冲突,我想用WIN32_LEAN_AND_MEAN包装windows.h的包含(我在windows.h之后取消定义它,以免干扰包含我的标头的应用程序)。当不包含winsock2.h 时,这样做会导致timeval 未定义。包括 time.h 也没有定义 timeval 。
如何获得 timeval 定义 (a) 而不必包含winsock2.h,(b) 不要求包含我的标头的应用程序在我的标头之前包含winsock2.h,(c) 允许应用程序在需要时包含winsock2.h ,以及(d)不必自己定义 timeval,因为它可能已经由父应用程序包含的标头定义?
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请记住,WIN32_LEAN_AND_MEAN 是编译器性能优化。使用它可以使您的应用程序编译速度更快,但代价是省略 Windows API 中一些较少使用的部分。
您可以使用一种更细粒度的禁用(NOIMM 等),但这些对编译时间的影响甚至比 LEAN_AND_MEAN 更小。
除非您的项目非常大并且编译时间长且繁重,否则我会停止使用 WIN32_LEAN_AND_MEAN。
马丁
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.
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.
Unless your project is very large and has long, onerous compile times I would simply stop using WIN32_LEAN_AND_MEAN.
Martyn
我没有定义任何一个 LEAN_AND_MEAN,而是在包含 windows.h 之前对每个“NO”明确执行以下操作,
这允许我使用相同的 StdAfx.h。使用 VS2010,我可以在 windows.h 之后包含 Winsock2,而不会发生冲突。
Stdafx 稍长一些,但清楚地记录了包含和排除的内容。
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
This allows me to use the same StdAfx.h. With VS2010 I can include Winsock2 after windows.h without conflicts.
Stdafx is a little longer, but clearly documents what is included and excluded.
这就是我处理
winsock2.h
问题的方法(假设为 Visual C++):/D_WINSOCKAPI_
传递给编译器,以便没有任何 Winsock 头文件被隐式包含。winsock2.h
时,我通过一个代理头文件来实现,该文件为我提供了一些预处理器的魔力。这就是我的头文件的工作原理:
如果您这样做,则无需
#define WIN32_LEAN_AND_MEAN
,并且仅在需要时才可以使用winsock2.h
。This is how I handle problems with
winsock2.h
(assuming Visual C++):/D_WINSOCKAPI_
to the compiler on the command line so that no winsock header files are ever implicitly included.winsock2.h
, I do it via a proxy header file that does some preprocessor magic for me.This is how my header file works:
If you do this then you don't need to
#define WIN32_LEAN_AND_MEAN
and you can usewinsock2.h
only when needed.我认为你想要的不可能。 timeval 结构体在winsock.h 和winsock2.h 中定义,因此您必须包含其中之一才能获取定义。如果不定义WIN32_LEAN_AND_MEAN,windows.h将包含winsock.h;这使得不可能包含winsock2.h。如果定义 WIN32_LEAN_AND_MEAN,则不会自动包含两个 Winsock 头文件,因此您无法获得 timeval 的定义。我认为最好的选择是要求应用程序显式包含 winsock 标头之一。
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.