包括 stdafx.h winsock2 重新定义错误

发布于 2024-08-17 01:41:14 字数 528 浏览 5 评论 0原文

当我包含 Winsock2.h 时,我收到大约 60 个重新定义错误。我四处寻找了一下,找到了一些建议,包括包含 windows.h 在内的 Winsock2.h b4。我这样做并清除了错误。我的问题和问题恰恰涉及我应该如何去做这件事。我没有明确包含 windows.h,它是在 stdafx.h 或 stdafx.cpp 中为我完成的。

我立即添加了包含winsock2.h b4包含Windows.h在stdafx.h中。这是解决这个问题的正确方法还是有更好的方法?

根据program_name.rc中的注释判断,我收集stdafx.h中包含的windows.h可能由于某些选项或配置参数而被放置在那里,但我无法找到此参考。有没有办法指定 stdafx.h 中包含哪些文件?

顺便说一句,WIN32_LEAN_AND_MEAN 是通过调用 stdafx.h 中的 windows.h 定义的。

我正在使用 Visual c++ 6.0 和“Windows Server 2003 PSDK” 该程序是直接的 c++,没有 mfc,没有 net,只是普通的 vanilla。

When I include winsock2.h, I get about 60 redefinition errors. I hunted around a bit a found some advice to include winsock2.h b4 including windows.h. I did that and that cleared up the errors. My problem and question concerns exactly how I should go about doing this. I did not explicitly include windows.h, it was done for me in stdafx.h or stdafx.cpp.

I added the include winsock2.h immediately b4 the include Windows.h in stdafx.h. Is this the right way to go about this or is there a better way?

Judging by a comment in program_name.rc I gather the windows.h include in stdafx.h may have been placed there as a result of some option or configuration parameter but I was unable to find this reference. Is there some way to specify what files are included in stdafx.h?

BTW, WIN32_LEAN_AND_MEAN was defined b4 calling windows.h in stdafx.h.

I am using Visual c++ 6.0 and 'Windows Server 2003 PSDK' The program is straight c++, no mfc, no net, just plain vanilla.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

抚笙 2024-08-24 01:41:14

您可以将几乎所有您想要的内容放入 stdafx.h 中。在windows.h 之前添加winsock2.h 的#include 当然没问题。我将移动 WIN32_LEAN_AND_MEAN 标头,以便在包含任何其他标头之前对其进行定义:

#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>

stdafx.h 是预编译标头的可怕名称。我不知道为什么 Visual Studio 仍然对所有自动生成的项目使用它。它给预编译头赋予了一种不应有的神秘感。在我的项目中,我通常设置预编译头以使用“precompiled.h”和“precompiled.cpp”。

Noel Llopis 有一篇关于预编译头的精彩文章 - 'The Care 和如果您想了解更多有关此处发生的情况的背景信息,请参阅预编译标头的馈送'。

You can put pretty much whatever you want into stdafx.h. It's certainly fine to add your #include for winsock2.h before the windows.h. I'd move the WIN32_LEAN_AND_MEAN header so that it's defined before you include any other headers:

#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>

stdafx.h is a horrendous name for the precompiled header. I have no idea why Visual Studio still uses this for all the autogenerated projects. It gives the precompiled header an undeserved air of mystery. In my projects I usually set up the precompiled header to use 'precompiled.h' and 'precompiled.cpp'.

Noel Llopis has a great article on precompiled headers - 'The Care and Feeding of Precompiled Headers' if you want a bit more background info on what's going on here.

从此见与不见 2024-08-24 01:41:14

那应该没问题。如果您查看winsock2.h,您可以看到它包含windows.h(如果尚未包含)。

That should work okay. If you look in winsock2.h, you can see that it includes windows.h if it hasn't already been included.

难忘№最初的完美 2024-08-24 01:41:14

您需要确保 WinSock2.h 包含在 windows.h 之前,确保无论您在何处包含 WinSock2.h,它都包含在 stdafx.h 和/或 windows.h 之前

You need to make sure that WinSock2.h is included BEFORE windows.h, make sure that wherever you're including WinSock2.h, it is included before stdafx.h and/or windows.h

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