can't link ws2_32.lib

I've been looking at this and other forums for three days, and see that this issue seems to come up a lot, but I have yet been able to find a solution. I'm using VS for C++ and trying to write a tutorial program on sockets. I have been stuck on linking ws2_32.lib to the project. I have added ws2_32 to dependencies, and tried to add to my header file- but that didn't work. WSAStartup(...) on line 11 is underlined in red, as is HIBYTE on line 22. Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <Winsock2.h>
#include <iostream>

int main()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	int wsaerr;

	wVersionRequested = MAKEWORD(2, 2);
	wsaerr = wsaStartup(wVersionRequested, &wsaData);
	
	if (wsaerr != 0)
	{
		printf("The Winsock dll not found.\n");
		return 0;
	}
	else
	{
		printf("The Winsock dll was found.\n");
		return 0;
	}
	
	if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
	{
		printf("The dll does not support the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
		WSACleanup();
		return 0;
	}
	else
	{
		printf("The dll does support the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
		printf("The highest version this dll can support: %u.%u!\n", LOBYTE(wsaData.wHighVersion), HIBYTE(wsaData.HighVersion));
		WSACleanup();
		return 0;
	}
}


Here's my errors on compiling:

1
2
3
4
5
6
7
8
1
1>  networking.cpp
1>c:\users\ed\documents\visual studio 2010\projects\networking\networking\networking.cpp(14): error C3861: 'wsaStartup': identifier not found
1>c:\users\ed\documents\visual studio 2010\projects\networking\networking\networking.cpp(36): error C2039: 'HighVersion' : is not a member of 'WSAData'
1>          c:\program files (x86)\microsoft sdks\windows\v7.0a\include\winsock2.h(360) : see declaration of 'WSAData'
1>c:\users\ed\documents\visual studio 2010\projects\networking\networking\networking.cpp(37): error C2143: syntax error : missing ';' before ':'
1>c:\users\ed\documents\visual studio 2010\projects\networking\networking\networking.cpp(37): error C2143: syntax error : missing ';' before ':'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Any help would be highly appreciated, that is after you stop laughing. OBTW, the path that I'm getting the ws2_32.lib file from is: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib, in case that matters.

Thanks,

harpnut
Last edited on
You wrote wsaStartup instead of WSAStartup and HighVersion instead of wHighVersion. Check a reference next time before coming here. I think the other errors will go away once you fix these. I don't see what else could be causing them.
You used wHighVersion right next to HighVersion, so you should have spotted that error easily enough.

And you used WSA with Cleanup, which should have made you wonder about Startup. APIs tend to use a uniform prefix. In this case, all (or at least most of) the Microsoft extensions to the standard socket API use a WSA prefix.

Andy

PS You have too many returns!
Last edited on
Topic archived. No new replies allowed.