简体   繁体   中英

undefined reference to imp_getaddrinfo

I have been learning how to program networking in windows via C++ , it's going well except I have encountered a problem of which I was unable to solve for about 3-4 days now, I am using Dev-C++ 5.5.3(Orwell) , the compiler is TDM-GCC 4.7.1 . I've added "-lwsock32" (without the quotes) to the linker parameters. all works well except the "freeaddrinfo" and "getaddrinfo" , here is what it says on 2 of these functions.

undefined reference to ` imp _getaddrinfo@16'

undefined reference to ` imp _freeaddrinfo@4'

I have read somewhere that it it requires me to define the version of windows I'd like to use, so I defined the _WINNT_WIN32 0x0601 as required, but with no avail. Here is my code(shortened):

#define _WINNT_WIN32 0x0601
#include <ws2tcpip.h>
#include <winsock2.h>
#include <stdio.h>

#define DEFAULT_PORT "27015"
// server

int main()
{
    WSADATA wsaData;                                
    ZeroMemory(&wsaData,sizeof(wsaData));
    int nResult = WSAStartup(MAKEWORD(2,2),&wsaData);
    if(nResult != 0)
     {
                printf(TEXT("WSAStartup function failed, value: %d\n"),nResult);
                Sleep(5000);
                return 0001;
     }
    else
      printf(TEXT("WSAStartup function has succeeded! value: %d\n"),nResult);
    struct addrinfo *result = NULL, *ptr = NULL, hints;
    ZeroMemory(&hints,sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE;
    nResult = getaddrinfo(NULL,DEFAULT_PORT,&hints,&result);
    if(nResult != 0)
    {
        printf("getaddrinfo did not return 0... failure...");
        WSACleanup();
        return 0002;
    }
    SOCKET ListenSocket = INVALID_SOCKET;
    ListenSocket = socket(result->ai_family,result->ai_socktype,result->ai_protocol);
    if (ListenSocket == INVALID_SOCKET)
    {
        printf("Error at socket():%ld\n",WSAGetLastError());
        freeaddrinfo(result);
        WSACleanup();
        return 0003;
    }
    nResult = bind(ListenSocket, result->ai_addr,(int)result->ai_addrlen);
    if(nResult == SOCKET_ERROR) 
    {
        printf("bindfailed with error %d\n",WSAGetLastError());
        freeaddrinfo(result);
        closesocket(ListenSocket);
        WSACleanup();
        return 0004;
    }
    freeaddrinfo(result);
    if(listen(ListenSocket,SOMAXCONN) == SOCKET_ERROR)
    {
        printf("lISTEN FAILED WITH ERROR %LD\n",WSAGetLastError());
        closesocket(ListenSocket);
    }
    return 1337^9001;
} 

Thanks.

确保在预处理器定义中定义_WIN32_WINNT,并使用-lWs2_32 参见此处

This is a classic case of an unresolved external. The function you are trying to use is getaddrinfo . Scroll down to the bottom of that documentation link to the section headed requirements. It tells you that you need to include Ws2tcpip.h to declare the function, and you've already done that. But it also tells you that you need to use the Ws2_32.lib import library to provide the definition of the function. That step is what you have missed.

So to solve the problem you need to link the Ws2_32 library, by adding -lWs2_32 to your command line options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM