I've been trying to get started with sockets programming. However, when I try to use the getaddrinfo and freeaddrinfo functions, my linker complains that they are "not declared in this scope" and that they are "unused variables". I have checked the header (ws2tcpip.h) and the headers appear to be declared correctly. I have tried linking to libws2_32.a ( the Code::Blocks version of ws2_32.lib) but the linker reacts the same whether or not the library is included.

Why are these functions not working?

I've been trying to get started with sockets programming. However, when I try to use the getaddrinfo and freeaddrinfo functions, my linker complains that they are "not declared in this scope" and that they are "unused variables". I have checked the header (ws2tcpip.h) and the headers appear to be declared correctly. I have tried linking to libws2_32.a ( the Code::Blocks version of ws2_32.lib) but the linker reacts the same whether or not the library is included.

Why are these functions not working?

Hard to tell when you don't show us the context in which they are used.

Yeah I guess that was a pretty important detail that I forgot. My apologies.

#include <ws2tcpip.h>
#include <iostream>

using namespace std;


int main(int argc, char *argv[])
{
    //The winsocks initialization code...
/*----------------------------------------------------------------------------*/
    WSADATA wsaData;


    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
        {
                 //if WSAStartup returns anything but 0, there was an error...
                 fprintf(stderr, "WSAStartup failed.\n");
                 system("PAUSE");
                 WSACleanup();
                 exit(1);
        }
/*----------------------------------------------------------------------------*/



/*APPLICATION CODE*/

char *target_addr = "www.google.com";
char *target_port = "80";

int status;
struct addrinfo info;
struct addrinfo *results;  // will point to the results

memset(&info, 0, sizeof info); // make sure the struct is empty
info.ai_family = AF_UNSPEC;     // don't care IPv4 or IPv6
info.ai_socktype = SOCK_STREAM; // TCP stream sockets
//hints.ai_flags = AI_PASSIVE;     // fill in my IP for me

if (status = getaddrinfo(target_addr, target_port, &info, &results) == -1) {
    fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
    exit(1);
}

// servinfo now points to a linked list of 1 or more struct addrinfos
int s;

s = socket(results->ai_family, results->ai_socktype, results->ai_protocol);

// ... do everything until you don't need servinfo anymore ....

freeaddrinfo(results); // free the linked-list

//CLEANUP CODE
        WSACleanup(); // closes up the winsocks library
        printf("Program was successfully executed.\n");
        return EXIT_SUCCESS;
}

The program is set to link with libws2_32.a.

Hopefully that will make it a little easier to figure out the problem.

I wonder if the

#if (_WIN32_WINNT >= 0x0501)

isn't working correctly... (In ws2tcpip.h)

Yeah, that was just about the first thing I tried when the errors started popping up. It didn't do anything then. So I spent a week trying to fix the ws2_32 library. But I just realized, the first time I tried to fix the header, I never hit save. I forgot that the only files that are saved upon compiling are the files in the current project.

In any case, removing that particular line did the trick (now that I actually saved the file). Thanks for helping me figure it out!

If the compiler is cring is not because linker errors....

Your error is alredy solved in other part of internet, but i dont remember where... please add at the beginning of your code

#define _WIN32_WINNT 0x501
Comments
2
This question has already been answered. Start a new discussion instead.