프로그래밍/WIN32 API

Redefinition errors when including winsock2.h

Subi Lee 2010. 7. 9.
반응형

Redefinition errors when including winsock2.h

Problem

You receive error messages similar to the following when trying to include winsock2.h header

"WinSock2.h(109): error C2011: 'fd_set' : 'struct' type redefinition"
"WinSock2.h(144): warning C4005: 'FD_SET' : macro redefinition"

Solution

There are a couple of possible solutions
  1. Make sure you include winsock2.h before windows.h
  2. Define WIN32_LEAN_AND_MEAN before including windows.h

Explanation

By default windows.h includes Winsock 1.1 header file winsock.h. For reasons that were never explained Microsoft decided to make winsock.h and winsock2.h incompatible. Some of the types defined in winsock.h are redefined in winsock2.h. This causes the errors you see. When winsock2.h is already included windows.h does not include winsock.h. Same happens when WIN32_LEAN_AND_MEAN is defined. That's why the solutions above work. 
Note that defining WIN32_LEAN_AND_MEAN has other consequences. The purpose of this macro is explained here

반응형

댓글