Getting error undefined reference to `getaddrinfo' on Linux
When I try to compile my small C program, I get the error msg: : undefined reference to `getaddrinfo'. I have included the netdb.h and socket.h header files too. But the error is in linker.
I see a query already on stackoverflow, but that was on Windows. I am trying it on a Linux machine. How do I make the compilation succeed ?
Thanks !
Here is the code:
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
int main() {
int status;
struct addrinfo hints;
struct addrinfo *servinfo; // will point to the results
memset(&hints, 0, sizeof hints); // make sure the struct is empty
hints.ai_family = AF_UNSPEC; // don't care IPv4 or IPv6
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
hints.ai_flags = AI_PASSIVE; // fill in my IP for me
if ((status = getaddrinfo(NULL, "80", &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo error: \n");
exit(1);
}
printf("\n %d %d %d %d %d %s",servinfo->ai_flags, servinf开发者_JAVA百科o->ai_family, servi nfo->ai_socktype, servinfo->ai_protocol, servinfo->ai_addrlen, servinfo->ai_canonname);
return 0;
}
gcc test5.c
/tmp/ccrFX43l.o(.text+0x4c): In function main':
: undefined reference to
getaddrinfo'
collect2: ld returned 1 exit status
精彩评论