views:

46

answers:

3

Getting heaps (105) of redefine & syntax errors when trying to compile my VS2008 c++ project with <winsock2.h> included. Running Windows 7 64bit. I have googled and searched and the answer seems pretty uniform but it doesn't seem to work for me.

Tried putting #include <winsock2.h> before #include <windows.h>. Also tried not including <windows.h> (as winsock2 includes it automatically if missing).

Tried with #define WIN32_LEAN_AND_MEAN and/or #define _WINSOCKAPI_ prior.

In the linker -> additional dependencies I have referenced ws2_32.lib. Also tried #pragma comment(lib, "ws2_32.lib").

I rebuild the whole solution just in case.

Usual error (shared by many it seems):

Error   5   error C2011: 'sockaddr' : 'struct' type redefinition    c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h  206 RallyAction
Error   6   error C2143: syntax error : missing '}' before 'constant'   c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h  384 RallyAction
Error   7   error C2143: syntax error : missing ';' before 'constant'   c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h  384 RallyAction
Error   8   error C2059: syntax error : 'constant'  c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h  384 RallyAction

...etc,etc.

Any suggestions would be appreciated.

+1  A: 

I have a "WinsockWrapper.h" header which helps me ensure consistency... The gist of it is;

#ifndef _WINDOWS_
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
#endif

#include <winsock2.h>

#pragma comment(lib, "ws2_32.lib")

Include this before anything else which might pull in windows.h in any file which needs winsock2.h

This, IMHO means putting it at the top of most files that need winsock2.h ...

Make sure your precompiled header, if you're using it, also has this first.

Len Holgate
A: 

This is a well-known problem. Somehow MS made winsock.h and winsock2.h incompatible. This problem exists from MSVC 6.

It's just one of the files you include also includes winsock.h, whereas you later include winsock2.h

One of the ways to fix this is including winsock2.h first. Even before windows.h (it'll include windows.h automatically)

valdo
A: 

Turns out < windows.h> was called elsewhere in project (separate .h file) and prior to the above .h file. That's what I get for coding late. Thanks for the help, appreciated.

I replaced that < windows.h> calling with < winsock2.h> and it's working fine now.

include < mmsystem.h> starting randomly giving me problems after that though (every other compile). I got it encapsulated with #ifdef _WIN32 but still it seems random. Having saved the project after the last time it compiled successfully (every other time) and restarted VS2008 seems to have 'fixed' it. The error was a internal linker error (got the mms lib referenced).

Wollan