trouble with Ws2tcpip.h file

Sep 5, 2012 at 4:59am
Hi -

I've reduced this code fragment as much as I can. It began as demo software for a socket client. Specifically, despite including the Ws2tcpip.h file, my compiler is still giving me the error:

test.cpp:6: error: 'getaddrinfo' was not declared in this scope


Here's the file giving the error:

1
2
3
4
5
6
7
8
9
#include<string>
#include<Ws2tcpip.h>            //used instead of "Winsock2.h"

int myStartupClient(std::string ipAddress, std::string portNumber, struct addrinfo giveHints, struct addrinfo **receiveServerInfo)
{
    int status = getaddrinfo(ipAddress.c_str(), portNumber.c_str(), (const struct addrinfo *) &giveHints, receiveServerInfo);
    return 0;
}


The toolchain is mingw, running on Windows 7. Any ideas what I'm doing wrong?

Thanks.
Sep 5, 2012 at 10:07am
closed account (DSLq5Di1)
ws2tcpip.h is a supplement to the winsock2.h, not a replacement.
Sep 5, 2012 at 3:42pm
I get the same compiler error with or without including winsock2.h.

In the header file, I noticed this odd bit of code:

1
2
3
4
5
6
7
8
9
#if (_WIN32_WINNT >= 0x0501)
void WSAAPI freeaddrinfo (struct addrinfo*);
int WSAAPI getaddrinfo (const char*,const char*,const struct addrinfo*,
		        struct addrinfo**);
int WSAAPI getnameinfo(const struct sockaddr*,socklen_t,char*,DWORD,
		       char*,DWORD,int);
#else
/* FIXME: Need WS protocol-independent API helpers.  */
#endif 


Evidently, an include file in my mingw distribution was setting this value too low. I defined it as 0x0601 for Windows 7, and I'm past that problem.

Now, though, I'm getting a linker error. I'm linking against this library:

C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib\wsock32.lib

and getting this error:

debug/client.o: In function `Z13startupClientSsSs8addrinfoPPS_':
C:\Users\Mike\Desktop\Client-Server\Client-Server for Windows OS\Regular Connection\client/client.cpp:137: undefined reference to `getaddrinfo@16'


Am I somehow picking up the wrong version of this routine or something? I get these errors even if I set the macro to 0x501.
Sep 5, 2012 at 5:48pm
closed account (DSLq5Di1)
I think you are linking to the old winsock library, it should be ws2_32.lib if memory serves.
Sep 5, 2012 at 6:08pm
You are 100% right. I guess the example I pulled off the net is kind of old. Thanks, sloppy9.
Topic archived. No new replies allowed.