C++ Redefinition Header Files (Winsock2.H)

Redefinition (WinSock2.h, winsock.h, ws2def.h) when using OR-Tools in Qt

Adding a DEFINES += _WINSOCKAPI_ in the Qt project config file (*.pro) has solved it.

VC++ C2011 redefinition errors - unused header files

The issue seems to have mostly been based on the horrible file structure of the original large project.
Trying to create a new project within the solution to replicate another project as a GUI instead of Console project meant that the compilation of the project was doubling up on many files.

I found another post (can't find it right now) that stated that when a Visual Studio solution compiles, all of the project files are combined, much like all the #include statements in a .c/.cpp/.h file really just copy all of those files into the file which #included them.

My solution was to create a copy of the large project (for backup purposes), and in the copied project, remove the original Console project and only include the UI Project. Files are not longer doubling up because they don't exist in both projects anymore, just the one still existing in the solution.

Cannot include both files (WinSock2, Windows.h)

Make sure that <windows.h> doesn't include <winsock.h> (which provides many of the same declarations as <winsock2.h>). In the <winsock2.h> file on my system there is this line:

#define _WINSOCKAPI_   /* Prevent inclusion of winsock.h in windows.h */

The _WINSOCKAPI_ include guard may be an internal implementation detail, but as a practical solution I would rely on it, just defining this symbol before including <windows.h>, e.g. in the compiler invocation (which for an IDE means in the IDE project settings).

Alternatively you can try to always include <winsock2.h> before <windows.h>, in order to establish the relevant include guard whatever it is (but this is I think much more fragile than just assuming that the above guard is practically well-defined);

or you can define WIN32_LEAN_AND_MEAN, which prevents <windows.h> from including <winsock.h> but also some other headers (listing from source on my system those are <cderr.h>, <dde.h>, <ddeml.h>, <dlgs.h>, <lzexpand.h>, <mmsystem.h>, <nb30.h>, <rpc.h>, <shellapi.h>, <winperf.h>, <wincrypt.h>, <winefs.h>, <winscard.h>, <winspool.h>, <ole2.h>, and <commdlg.h>). I do not recommend relying on WIN32_LEAN_AND_MEAN optimization for correctness.

I.e., minimum:

#undef UNICODE
#define UNICODE
#undef _WINSOCKAPI_
#define _WINSOCKAPI_
#include <windows.h>
#include <winsock2.h>

auto main()
-> int
{}

Why do I get a flood of compiler errors when I include WinSock2.h?

I guess you may have a problem in the order of inclusions.

You are probably getting many errors along the lines of:

1>c:\program files (x86)\windows kits\8.1\include\um\winsock2.h(2373): error C2375: 'WSAStartup': redefinition; different linkage
1> c:\program files (x86)\windows kits\8.1\include\um\winsock.h(867): note: see declaration of 'WSAStartup'

That's because <windows.h> includes <winsock.h> by default, and <winsock.h> provides many declarations that overlap with those in <winsock2.h>, which causes errors when <winsock2.h> is included after <windows.h>.

So, you may want to include <winsock2.h> before <windows.h>:

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

Or, as an alternative, you may try to define _WINSOCKAPI_ to prevent the inclusion of <winsock.h> in <windows.h> with this preprocessor #undef-#define-#include "dance":

#undef _WINSOCKAPI_
#define _WINSOCKAPI_ /* prevents <winsock.h> inclusion by <windows.h> */
#include <windows.h>
#include <winsock2.h>

I have to say that the definition of _WINSOCKAPI_ macro to interfere in the ordinary header inclusion guard mechanics to prevent <windows.h> to include <winsock.h> sounds like an implementation-details-based fragile "hack", so I would probably prefer the first option.

But all in all this order of inclusion bug sounds to me like a bug in the Win32's headers, so the best thing would be for Microsoft to fix that.

EDIT

As suggested in the comments, a further alternative may be to #define WIN32_LEAN_AND_MEAN before including <windows.h>. However, please note that this would prevent the inclusions of other Windows headers as well.

P.S.

If you are using precompiled headers ("stdafx.h" in the newly showed code in your question), you may want to pay attention to order of inclusions in there as well.

Winsock redefinition errors

Your problem is that by including Windows.h, you are also already including winsock.h. It is here your problem arises as including winsock2.h or ws2tcpip.h will attempt to redefine some of the definitions in winsock.h

By using #define WIN32_LEAN_AND_MEAN before your Windows.h include you stop the compiler from including a lot of the extra stuff that comes with Windows.h



Related Topics



Leave a reply



Submit