Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2008 Compile error with <winsock2.h>

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.

like image 888
Wollan Avatar asked Oct 31 '10 02:10

Wollan


3 Answers

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.

like image 99
Len Holgate Avatar answered Oct 17 '22 23:10

Len Holgate


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)

like image 24
valdo Avatar answered Oct 17 '22 23:10

valdo


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).

like image 21
Wollan Avatar answered Oct 17 '22 22:10

Wollan