C++ Redefinition Header Files (winsock2.h)

158,881

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 before windows.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 with WinSock2.h is included, windows.h can be already included from some other header/implementation file.

  • Define WIN32_LEAN_AND_MEAN before windows.h or project wide. But it will exclude many other stuff you may need and you should include it by your own.

  • Define _WINSOCKAPI_ before windows.h or project wide. But when you include WinSock2.h you get macro redefinition warning.

  • Use windows.h instead of WinSock2.h when winsock.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.

Share:
158,881

Related videos on Youtube

akif
Author by

akif

Updated on July 14, 2021

Comments

  • akif
    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
      Kiran Kumar over 14 years
      You are already using #pragma once, so it should be included only once.
    • Svetlozar Angelov
      Svetlozar Angelov over 14 years
      Your compiler doesn't support pragma once?
    • akif
      akif over 14 years
      I'm using Visual Studio 2008, why is then <winsock2.h> included twice?
    • Svetlozar Angelov
      Svetlozar Angelov over 14 years
      It might be included twice from some of the included headers from MyClass.h
    • akif
      akif over 14 years
      then how to remedy the problem?
    • Kiran Kumar
      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
      Svetlozar Angelov over 14 years
      winsock2 and winsock have common structures. You have to include just one of them, not the both
  • akif
    akif over 14 years
    #pragma once and include guards are same things aren't they?
  • Svetlozar Angelov
    Svetlozar Angelov over 14 years
    I 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
    akif over 14 years
    Well, I have #pragma once which afaik is the same header guards
  • akif
    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
    Kiran Kumar over 14 years
    @Manzoor: What is the problem your trying address then?
  • akif
    akif over 14 years
    The 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
    Timo Geusch over 14 years
    They 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
    akif over 14 years
    I meant they are built for the same purposes :)
  • ntcong
    ntcong over 14 years
    #pragma once is non-standard, afaik
  • ntcong
    ntcong over 14 years
    #pragma once relies on compiler, it's not the same as header guard, which is more portable
  • KitsuneYMG
    KitsuneYMG over 14 years
    By 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
    DevSolar over 14 years
    The '#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
    DevSolar over 14 years
    Above 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
    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
    DevSolar over 14 years
    Then 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
    akif over 14 years
    I'm not including <windows.h> at all, I know <winsock2.h> does its for me.
  • ntcong
    ntcong over 14 years
    pragma one is known to be faster than header guard, but it's not standard. So sometimes developer use both of them.
  • ransomenote
    ransomenote over 14 years
    For 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
    Steve Jessop over 14 years
    @DevSolar: you're way out of date. GCC has supported #pragma once for years.
  • DevSolar
    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
    Thomi over 14 years
    a symbolic link or NTFS junction will also cause the system to break.
  • Steve Jessop
    Steve Jessop over 14 years
    I 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
    DevSolar over 14 years
    Hrr, 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
    Steve Jessop over 14 years
    Unfortunately, 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
    Thomi over 14 years
    When 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
    Colin Desmond over 14 years
    Is <windows.h> being included in stdafx.h?
  • adamfisk
    adamfisk over 12 years
    This solution fixed the issue for me on VS 2010 with SDK 7.1. Thanks pingw33n!
  • Ava
    Ava about 12 years
    I 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
    Jonatan Cloutier almost 12 years
    WIN32_LEAN_AND_MEAN was the solution for me tanks a lot
  • Lucky Luke
    Lucky Luke over 11 years
    Ran 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
    icecream almost 11 years
    I 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
    Nitzan Tomer over 10 years
    thanks 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
    phyatt about 10 years
    On Qt, in the .pro file, it looks like this: DEFINES += _WINSOCKAPI_
  • Leif Gruenwoldt
    Leif Gruenwoldt over 9 years
    @phyatt: you should turn that into an answer, if you don't I will!
  • phyatt
    phyatt over 9 years
    @LeifGruenwoldt go for it! Glad I could help.
  • AndroC
    AndroC over 7 years
    Im using VS2013. Putting _WINSOCKAPI_ in Project Properties > Configuration Properties > C/C++ > Preprocessor > Preprocessor definitions for both Debug and Release configurations did the trick for me.
  • Paweł Stankowski
    Paweł Stankowski over 7 years
    About _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
    sqr163 almost 7 years
    @pingw33n Thank you! helped to compile some C++ code with VS2014
  • Lorien Brune
    Lorien Brune over 6 years
    If you use #define _WINSOCKAPI_, you may also need #define _WINSOCK_DEPRECATED_NO_WARNINGS, depending on your circumstances.
  • Elmue
    Elmue over 3 years
    In my case including "windows.h" as the LAST of all includes removed the compiler error.