Undefined link to getaddrinfo

I have been getting this error for quite some time now, and Google also did not help much.

I am new to Winsock programming and try to learn from online resources. I am trying to create a simple server using the information on the MSDN website. Whenever I compile the code (MinGW), I get the error indicated in the header ( Undefined reference to getaddrinfo ). Below is the code:

 #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif #define WINVER WindowsXP #include <windows.h> #include <winsock2.h> #include <winsock.h> #include <ws2tcpip.h> #include <stdio.h> int main() { WSADATA wsaData; int iResult; iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != 0) { printf("WSAStartup failed: %d\n", iResult); return 1; } #define DEFAULT_PORT "27015" 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; // Resolve the local address and port to be used by the server iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result); if (iResult != 0) { printf("getaddrinfo failed: %d\n", iResult); WSACleanup(); return 1; } return 0; } 

I am compiling the following command:

 gcc msdn_np.c -o msdn_np.exe -lWS2_32 
+7
source share
3 answers

Isn't that a problem like here?

http://programmingrants.blogspot.com/2009/09/tips-on-undefined-reference-to.html

Basically, don't forget to establish a connection with Ws2_32.lib (a message from the linker, so this should be the reason), but you seem to be doing it already.

... if you are working with an old version of Windows software, say you have a version higher than XP by placing #define _WIN32_WINNT 0x0501 before including the headers (it is unlikely to be necessary now, but it may be).

These may be other simple problems. The usual (Unix) convention for libraries is to add them to lib. In the future, the value for -lWS32_32 will consist in finding a file named libWS32_32.a . He probably does not find it because he lacks the path to the directory containing the library. You can add -L followed by the path to the correct directory. In addition, you don’t even need to -l contact the library, just putting the full path into the library (in this case, the real file name as it appears in the file system) should also work.

The problem may also be related to paths. For example, problems may occur if the library path contains spaces. If so, you can try placing the library files in a directory with a simpler name.

Please use some feedback about your actual configuration (which contains the library file) and what you actually tried. You can also try setting the LIBS and LIBPATH environment variables (the simplest way is probably to do this from a makefile).

+10
source

http://msdn.microsoft.com/en-us/library/ms738520(v=vs.85).aspx

Take a look at the material under "comments." Perhaps you are trying to use the version of the ANSI-C function if you are not compiling as ANSI-C.

0
source

WindowsXP was defined in w32api.h , before #define WINVER you must #include <w32api.h> .

0
source

All Articles