C++ Redefinition Header Files (winsock2.h)
Solution 1
This problem is caused when including <windows.h>
before <winsock2.h>
. Try arrange your include list that <windows.h>
is included after <winsock2.h>
or define _WINSOCKAPI_
first:
#define _WINSOCKAPI_ // stops windows.h including winsock.h
#include <windows.h>
// ...
#include "MyClass.h" // Which includes <winsock2.h>
See also this.
Solution 2
As others suggested, the problem is when windows.h
is included before WinSock2.h
. Because windows.h
includes winsock.h
. You can not use both WinSock2.h
and winsock.h
.
Solutions:
Include
WinSock2.h
beforewindows.h
. In the case of precompiled headers, you should solve it there. In the case of simple project, it is easy. However in big projects (especially when writing portable code, without precompiled headers) it can be very hard, because when your header withWinSock2.h
is included,windows.h
can be already included from some other header/implementation file.Define
WIN32_LEAN_AND_MEAN
beforewindows.h
or project wide. But it will exclude many other stuff you may need and you should include it by your own.Define
_WINSOCKAPI_
beforewindows.h
or project wide. But when you includeWinSock2.h
you get macro redefinition warning.Use
windows.h
instead ofWinSock2.h
whenwinsock.h
is enough for your project (in most cases it is). This will probably result in longer compilation time but solves any errors/warnings.
Solution 3
Oh - the ugliness of Windows... Order of includes are important here. You need to include winsock2.h before windows.h. Since windows.h is probably included from your precompiled header (stdafx.h), you will need to include winsock2.h from there:
#include <winsock2.h>
#include <windows.h>
Solution 4
By using "header guards":
#ifndef MYCLASS_H
#define MYCLASS_H
// This is unnecessary, see comments.
//#pragma once
// MyClass.h
#include <winsock2.h>
class MyClass
{
// methods
public:
MyClass(unsigned short port);
virtual ~MyClass(void);
};
#endif
Solution 5
I ran into this problem when trying to pull a third party package which was apparently including windows.h somewhere in it's mess of headers. Defining _WINSOCKAPI_
at the project level was much easier (not to mention more maintainable) than wading through their soup and fixing the problematic include.
Related videos on Youtube
akif
Updated on July 14, 2021Comments
-
akif almost 3 years
How do I prevent from including header files twice? The problem is I'm including the in MyClass.h and then I'm including MyClass.h in many files, so it includes multiple times and redefinition error occurs. How to prevent?
I'm using #pragma once instead of include guards, and I guess that's fine.
MyClass.h:
// MyClass.h #pragma once #include <winsock2.h> class MyClass { // methods public: MyClass(unsigned short port); virtual ~MyClass(void); };
EDIT: Few of the errors I'm getting
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(91) : warning C4005: 'AF_IPX' : macro redefinition c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(460) : see previous definition of 'AF_IPX' c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(124) : warning C4005: 'AF_MAX' : macro redefinition c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(479) : see previous definition of 'AF_MAX' c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(163) : warning C4005: 'SO_DONTLINGER' : macro redefinition c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(402) : see previous definition of 'SO_DONTLINGER' c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(206) : error C2011: 'sockaddr' : 'struct' type redefinition c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(485) : see declaration of 'sockaddr' c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(384) : error C2143: syntax error : missing '}' before 'constant' c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(384) : error C2143: syntax error : missing ';' before 'constant' c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(384) : error C2059: syntax error : 'constant' c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(437) : error C2143: syntax error : missing ';' before '}' c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(437) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(437) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(518) : warning C4005: 'IN_CLASSA' : macro redefinition c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(287) : see previous definition of 'IN_CLASSA' c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(524) : warning C4005: 'IN_CLASSB' : macro redefinition c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(293) : see previous definition of 'IN_CLASSB' c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(530) : warning C4005: 'IN_CLASSC' : macro redefinition c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(299) : see previous definition of 'IN_CLASSC' c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(541) : warning C4005: 'INADDR_ANY' : macro redefinition c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(304) : see previous definition of 'INADDR_ANY' c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(543) : warning C4005: 'INADDR_BROADCAST' : macro redefinition c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(306) : see previous definition of 'INADDR_BROADCAST' c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(577) : error C2011: 'sockaddr_in' : 'struct' type redefinition c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(312) : see declaration of 'sockaddr_in' c:\program files\microsoft sdks\windows\v6.0a\include\winsock2.h(132) : error C2011: 'fd_set' : 'struct' type redefinition c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(68) : see declaration of 'fd_set' c:\program files\microsoft sdks\windows\v6.0a\include\winsock2.h(167) : warning C4005: 'FD_SET' : macro redefinition c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(102) : see previous definition of 'FD_SET' c:\program files\microsoft sdks\windows\v6.0a\include\winsock2.h(176) : error C2011: 'timeval' : 'struct' type redefinition c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(111) : see declaration of 'timeval' c:\program files\microsoft sdks\windows\v6.0a\include\winsock2.h(232) : error C2011: 'hostent' : 'struct' type redefinition c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(167) : see declaration of 'hostent' c:\program files\microsoft sdks\windows\v6.0a\include\winsock2.h(245) : error C2011: 'netent' : 'struct' type redefinition c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(180) : see declaration of 'netent' c:\program files\microsoft sdks\windows\v6.0a\include\winsock2.h(252) : error C2011: 'servent' : 'struct' type redefinition c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(187) : see declaration of 'servent' c:\program files\microsoft sdks\windows\v6.0a\include\winsock2.h(264) : error C2011: 'protoent' : 'struct' type redefinition c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(199) : see declaration of 'protoent'
-
Kiran Kumar over 14 yearsYou are already using #pragma once, so it should be included only once.
-
Svetlozar Angelov over 14 yearsYour compiler doesn't support pragma once?
-
akif over 14 yearsI'm using Visual Studio 2008, why is then <winsock2.h> included twice?
-
Svetlozar Angelov over 14 yearsIt might be included twice from some of the included headers from MyClass.h
-
akif over 14 yearsthen how to remedy the problem?
-
Kiran Kumar over 14 years@Manzoor: Is this the error you are getting "fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include <windows.h>"
-
Svetlozar Angelov over 14 yearswinsock2 and winsock have common structures. You have to include just one of them, not the both
-
-
akif over 14 years#pragma once and include guards are same things aren't they?
-
Svetlozar Angelov over 14 yearsI guess I am wrong (4 upvotes by now), but I think using include guards is the same as pragma once, you put them both?
-
akif over 14 yearsWell, I have #pragma once which afaik is the same header guards
-
akif over 14 years@Angelov: Yes, that's what I'm saying they are the same things. The problem is not with my header files, but I think <winsock2.h> itself doesn't have header guards or may be something else.
-
Kiran Kumar over 14 years@Manzoor: What is the problem your trying address then?
-
akif over 14 yearsThe problem is that I include <winsock2.h> in my header and then in another header I include my previous header file and so comes the redefinition of WinSock2 structures. Why?
-
Timo Geusch over 14 yearsThey are not quite the same - the header guards will prevent re-inclusion of the file at the preprocessor level, plus they're obviously a tad more portable than #pragma once.
-
akif over 14 yearsI meant they are built for the same purposes :)
-
ntcong over 14 years#pragma once is non-standard, afaik
-
ntcong over 14 years#pragma once relies on compiler, it's not the same as header guard, which is more portable
-
KitsuneYMG over 14 yearsBy definition #pragma is compiler dependent (non-standard). It may not work on all compilers. I know that visual studio accepts #pargma once. I'm not sure if gcc does. I know that include guards ALWAYS work. I use both #pragma once and include guards for maximum protability. It seems that MSVC has optimized handling of #pragma once and gcc has optimized handling of include guards. The only difference with my standard header is that #praga once is outside the include guards.
-
DevSolar over 14 yearsThe '#pragma' command is specified in the ANSI standard to have an arbitrary implementation-defined effect. In the GNU C preprocessor, '#pragma' first attempts to run the game 'rogue'; if that fails, it tries to run the game 'hack'; if that fails, it tries to run GNU Emacs displaying the Tower of Hanoi; if that fails, it reports a fatal error. In any case, preprocessing does not continue. -- Richard M. Stallman, The GNU C Preprocessor, version 1.34
-
DevSolar over 14 yearsAbove comment posted to stress that #pragma does "something", while #ifndef / #define / #endif is fully portable, standard compliant, and recommended practice. @ Manzoor Ahmed: Try to boil down your problem to the minimum amount of code required to reproduce it. It's a very useful practice for debugging such problems.
-
DevSolar over 14 years@ Svetlozar Angelov: I retained the #pragma once from the original code, not being 100% sure what it does, and simply wrapped the whole thing. Shouldn't hurt either way.
-
DevSolar over 14 yearsThen your problem is not related to multiple inclusions of MyClass.h. Again: Make a copy of your code, then methodically cut away files and source lines not necessary to reproduce the error, until you see the problem. Trust me, works every time.
-
akif over 14 yearsI'm not including <windows.h> at all, I know <winsock2.h> does its for me.
-
ntcong over 14 yearspragma one is known to be faster than header guard, but it's not standard. So sometimes developer use both of them.
-
ransomenote over 14 yearsFor me your code compiles ok with only
<winsock2.h>
in MSVC2008.<windows.h>
inclusion makes it generate identical compile errors as you provided. -
Steve Jessop over 14 years@DevSolar: you're way out of date. GCC has supported #pragma once for years.
-
DevSolar over 14 years@onebyone: That Richard Stallman quote was humor. You know? Like when you tell people that relying on undefined behaviour might format their hard drives...
-
Thomi over 14 yearsa symbolic link or NTFS junction will also cause the system to break.
-
Steve Jessop over 14 yearsI know, but the questioner isn't relying on undefined behaviour, and Stallman's quote is not applicable. He's relying on defined behaviour of his compiler (just not defined in the C++ standard). So #pragma doesn't do "something", it does exactly what it says in MS's documentation. Accuse me of humourlessness, I'll accuse you of derailing the conversation, someone can chip in with a Nazi analogy, and we'll have the usenet trifecta ;-)
-
DevSolar over 14 yearsHrr, hrr... I'm game. :-D You might want to note, though, that the original, unedited post I was referring to did not state the compiler being used...
-
Steve Jessop over 14 yearsUnfortunately, I've seen more than zero botched include guards, either where a typo means the guard doesn't actually work, or where files of the same name in different directories use the same token, or where the token used begins with a double underscore or underscore then capital-letter (and hence is non-portable just like #pragma once). So for inherently non-portable code, like anything using winsock.h, I was deeply untroubled by #pragma once right up to the point you said it was flakey. When does it fail, other than not being supported at all?
-
Thomi over 14 yearsWhen using
#pragma once
, the compiler takes the header file node name as the unique Id. This can fail if you have symbolic links or NTFS junctions in your source tree (more common than you might think), or even if you have a file of the same name in another system include directory (this has happened to me before when I has version 1 and version 2 of the same library installed to two different system include paths). Bottom line: for me, I prefer to have more control, and live with the occasional wetware mistake, rather than trust a compiler to do it for me. -
Colin Desmond over 14 yearsIs <windows.h> being included in stdafx.h?
-
adamfisk over 12 yearsThis solution fixed the issue for me on VS 2010 with SDK 7.1. Thanks pingw33n!
-
Ava about 12 yearsI had
#include <winsock2.h> #include <ws2tcpip.h> #include <windows.h>
in order and was getting winsock2,h file not found. Included#define _WINSOCKAPI_
above all 3 includes still the same error -
Jonatan Cloutier almost 12 years
WIN32_LEAN_AND_MEAN
was the solution for me tanks a lot -
Lucky Luke over 11 yearsRan into this problem in the past a few times and usually solved it by including winsock2.h first. Didn't know about WINSOCKAPI, and it made things easier in this project. Thanks pingw33n.
-
icecream almost 11 yearsI had trouble eliminating all inclusions of winsock.h even though I located all my own inclusions of windows.h. I finally solved it by adding
_WINSOCKAPI_
to the project options under Properties->C/C++->Preprocessor in that way making sure that it was defined before all compilation. -
Nitzan Tomer over 10 yearsthanks so much!!! I do not include any of those, but some other files I'm including do.. Your post made me change the order in which I include those files and it worked. You just saved me hours upon hours of searching, reading and swearing.
-
phyatt about 10 yearsOn Qt, in the .pro file, it looks like this:
DEFINES += _WINSOCKAPI_
-
Leif Gruenwoldt over 9 years@phyatt: you should turn that into an answer, if you don't I will!
-
phyatt over 9 years@LeifGruenwoldt go for it! Glad I could help.
-
AndroC over 7 yearsIm using VS2013. Putting
_WINSOCKAPI_
inProject Properties > Configuration Properties > C/C++ > Preprocessor > Preprocessor definitions
for both Debug and Release configurations did the trick for me. -
Paweł Stankowski over 7 yearsAbout
_WINSOCK_
solution: You shouldn't grt macro redefinition warning if both definitions are identical. The common bug is that people add definition to the project without setting any value and expect empty definition. However, if you add-D_WINSOCK_
to cmd line, it will set_WINSOCK_
to 1. To create empty definition,-D_WINSOCK_=
must be passed. -
sqr163 almost 7 years@pingw33n Thank you! helped to compile some C++ code with VS2014
-
Lorien Brune over 6 yearsIf you use
#define _WINSOCKAPI_
, you may also need#define _WINSOCK_DEPRECATED_NO_WARNINGS
, depending on your circumstances. -
Elmue over 3 yearsIn my case including "windows.h" as the LAST of all includes removed the compiler error.