当我尝试编译我的小C程序时,我收到错误消息: :未定义的引用`getaddrinfo'。我也包含了netdb.h和socket.h头文件。但错误在链接器中。
我在stackoverflow上看到了一个查询,但那是在Windows上。我在Linux机器上尝试它。 如何使编译成功?
谢谢!
以下是代码:
#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, servinfo->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):在函数main':
: undefined reference to
getaddrinfo'中
collect2:ld返回1退出状态