本文介绍了C++ 重定义头文件 (winsock2.h)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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 filesmicrosoft sdkswindowsv6.0aincludews2def.h(91) : warning C4005: 'AF_IPX' : macro redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(460) : see previous definition of 'AF_IPX'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(124) : warning C4005: 'AF_MAX' : macro redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(479) : see previous definition of 'AF_MAX'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(163) : warning C4005: 'SO_DONTLINGER' : macro redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(402) : see previous definition of 'SO_DONTLINGER'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(206) : error C2011: 'sockaddr' : 'struct' type redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(485) : see declaration of 'sockaddr'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(384) : error C2143: syntax error : missing '}' before 'constant'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(384) : error C2143: syntax error : missing ';' before 'constant'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(384) : error C2059: syntax error : 'constant'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(437) : error C2143: syntax error : missing ';' before '}'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(437) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(437) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(518) : warning C4005: 'IN_CLASSA' : macro redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(287) : see previous definition of 'IN_CLASSA'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(524) : warning C4005: 'IN_CLASSB' : macro redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(293) : see previous definition of 'IN_CLASSB'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(530) : warning C4005: 'IN_CLASSC' : macro redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(299) : see previous definition of 'IN_CLASSC'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(541) : warning C4005: 'INADDR_ANY' : macro redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(304) : see previous definition of 'INADDR_ANY'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(543) : warning C4005: 'INADDR_BROADCAST' : macro redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(306) : see previous definition of 'INADDR_BROADCAST'
c:program filesmicrosoft sdkswindowsv6.0aincludews2def.h(577) : error C2011: 'sockaddr_in' : 'struct' type redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(312) : see declaration of 'sockaddr_in'
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock2.h(132) : error C2011: 'fd_set' : 'struct' type redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(68) : see declaration of 'fd_set'
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock2.h(167) : warning C4005: 'FD_SET' : macro redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(102) : see previous definition of 'FD_SET'
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock2.h(176) : error C2011: 'timeval' : 'struct' type redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(111) : see declaration of 'timeval'
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock2.h(232) : error C2011: 'hostent' : 'struct' type redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(167) : see declaration of 'hostent'
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock2.h(245) : error C2011: 'netent' : 'struct' type redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(180) : see declaration of 'netent'
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock2.h(252) : error C2011: 'servent' : 'struct' type redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(187) : see declaration of 'servent'
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock2.h(264) : error C2011: 'protoent' : 'struct' type redefinition
c:program filesmicrosoft sdkswindowsv6.0aincludewinsock.h(199) : see declaration of 'protoent'
解决方案
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.
这篇关于C++ 重定义头文件 (winsock2.h)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!