Question

I have sample piece of code:

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

// link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")

//...

if ((err = getaddrinfo(hostname, service, &hints, &res)) != 0)
{
    printf("error %d\n", err);
    return 1;
}

which I try to compile on my 64bit Windows 7:

gcc -O0 -g3 -Wall -c -fmessage-length=0 -o src\sample.o ..\src\sample.c
..\src\sample.c: In function 'main':
..\src\sample.c:26:2: warning: implicit declaration of function 'getaddrinfo' [-Wimplicit-function-declaration]
gcc -o sample.exe src\sample.o -lws2_32
src\sample.o: In function `main':
C:\workspace\sample\Debug/../src/sample.c:26: undefined reference to `getaddrinfo'
collect2: ld returned 1 exit status

I get an error. How to make this work ?

Was it helpful?

Solution

If you're not targeting Windows 2000 or older, settings WINVER to 0x501 should let your code build correctly.

gcc -DWINVER=0x501 ...

(Or use a #define before you include any Windows header in your source.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top