gpt4 book ai didi

使用 getaddrinfo 编译错误

转载 作者:太空宇宙 更新时间:2023-11-04 02:14:46 24 4
gpt4 key购买 nike

我正在尝试学习套接字编程的基础知识,并且正在使用我找到的指南中的一些代码,但我遇到了编译错误。代码和错误如下所示

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

#define PORT "21467"

int main(void)
{
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_INET;
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
hints.ai_flags = AI_PASSIVE; // fill in my IP for me

if ((status = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
//fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
// servinfo now points to a linked list of 1 or more struct addrinfos
// ... do everything until you don't need servinfo anymore ....
freeaddrinfo(servinfo); // free the linked-list

return 0;
}

编译错误:

server.c: In function 'main':
server.c:16: warning: incompatible implicit declaration of built-in function 'memset'
server.c:24: warning: incompatible implicit declaration of built-in function 'exit'
Undefined first referenced
symbol in file
getaddrinfo /var/tmp//ccU0yRDe.o
freeaddrinfo /var/tmp//ccU0yRDe.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

如有任何帮助,我们将不胜感激!

最佳答案

如果你做 man memset ,您会看到手册页上写着 #include <string.h>是必须的。只需将其放在文件顶部即可。对 exit 做同样的事情

编辑

需要-lnsl -lsocket用于链接(假设是 linux)

关于使用 getaddrinfo 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9449277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com