使用 windows.h 和 WIN32_LEAN_AND_MEAN 时 timeval 未定义

发布于 2024-08-14 18:58:53 字数 310 浏览 6 评论 0原文

为了避免与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我一向站在原地 2024-08-21 18:58:53

请记住,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

绝不放开 2024-08-21 18:58:53

我没有定义任何一个 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

这允许我使用相同的 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

//  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 is a little longer, but clearly documents what is included and excluded.

伤感在游骋 2024-08-21 18:58:53

这就是我处理 winsock2.h 问题的方法(假设为 Visual C++):

  • 我将构建系统配置为始终在命令行上将 /D_WINSOCKAPI_ 传递给编译器,以便没有任何 Winsock 头文件被隐式包含。
  • 当我想要包含 winsock2.h 时,我通过一个代理头文件来实现,该文件为我提供了一些预处理器的魔力。

这就是我的头文件的工作原理:

#pragma push_macro("_WINSOCKAPI_")
#undef _WINSOCKAPI_
#include <winsock2.h> 
#pragma pop_macro("_WINSOCKAPI_")

如果您这样做,则无需#define WIN32_LEAN_AND_MEAN,并且仅在需要时才可以使用winsock2.h

This is how I handle problems with winsock2.h (assuming 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.

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.

旧时浪漫 2024-08-21 18:58:53

我认为你想要的不可能。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文