undefined reference to imp_getaddrinfo

c++, winapi, winsock2

Solution

Be sure that you define _WIN32_WINNT in your preprocessor definition, and use `-lWs2_32` see here

Problem

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.

Original source

Related problems