- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试编译这个程序,如 Beej 的网络编程指南 第 19 页所述。
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.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;
hints.ai_flags = AI_PASSIVE;
if ((status = getaddrinfo(NULL, "3490", &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;
}
除其他错误外,我还看到了
../main.c:8:18: error: storage size of ‘hints’ isn’t known
../main.c:13:19: error: ‘AI_PASSIVE’ undeclared (first use in this function)
../main.c:16:3: warning: implicit declaration of function ‘gai_strerror’
gcc 似乎没有与 netdb.h
链接。 Eclipse,我用来构建它的 IDE,可以毫不费力地找到文件。这是编译器命令:
gcc -O0 -g3 -pedantic -Wall -c -fmessage-length=0 -ansi -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.c"
添加 -lnetdb
不能解决问题。还有……
~> find /usr/include/ -name netdb.h
/usr/include/bits/netdb.h
/usr/include/gssrpc/netdb.h
/usr/include/netdb.h
/usr/include/rpc/netdb.h
我认为这些文件是预装在我的 openSUSE 主机上的。为什么 gcc 不检测 netdb.h
?还是我得出了错误的结论?
最佳答案
../main.c:8:18: error: storage size of ‘hints’ isn’t known
../main.c:13:19: error: ‘AI_PASSIVE’ undeclared (first use in this function)
../main.c:16:3: warning: implicit declaration of function ‘gai_strerror’It appears that gcc isn't linking with netdb.h....
这些不是链接错误,您不需要 netdb 共享对象文件(没有这样的野兽;netdb.h
只是定义数据结构和在您的代码中使用的宏)。
这些是编译器错误:gcc 提示因为你正在使用名称它无法识别 (AI_PASSIVE
) 和数据类型结构未知(struct addrinfo
)。
您提供的代码似乎是正确的,addrinfo
在 /usr/include/netdb.h
中定义。如果你编译它会发生什么像这样:
gcc -c main.c
你还会有同样的行为吗?如果是这样,请查看输出的:
gcc -E main.c
这会生成代码的预处理版本,其中包含所有#include
语句替换为它们的实际内容。你应该能够通过这个进行grep并查看编译器是否真的得到了/usr/include/netdb.h
如果它找到其他东西:
$ gcc -E foo.c | grep netdb.h | awk '{print $3}' | sort -u
在我的系统上产生:
"/usr/include/bits/netdb.h"
"/usr/include/netdb.h"
"/usr/include/rpc/netdb.h"
当您将 -ansi
添加到命令行时,您正在改变方式gcc
的行为方式会破坏 Linux 内核和许多系统头文件。 netdb.h
中的 addrinfo
定义受到保护像这样:
#ifdef __USE_POSIX
/* Structure to contain information about address of a service provider. */
struct addrinfo
{
int ai_flags; /* Input flags. */
int ai_family; /* Protocol family for socket. */
int ai_socktype; /* Socket type. */
int ai_protocol; /* Protocol for socket. */
socklen_t ai_addrlen; /* Length of socket address. */
struct sockaddr *ai_addr; /* Socket address for socket. */
char *ai_canonname; /* Canonical name for service location. */
struct addrinfo *ai_next; /* Pointer to next in list. */
};
// ...other stuff...
#endif
当您使用 -ansi
标志运行 gcc
时,这会取消定义__USE_POSIX
宏,因为受此保护的东西可能不会严格符合 ANSI 标准。对比一下就知道区别了这个:
gcc -E /usr/include/netdb.h
有了这个:
gcc -E -ansi /usr/include/netdb.h
只有前者包含addrinfo
结构。
关于c - netdb.h 链接不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7571870/
我尝试编译 .c 文件,但它返回错误: yay.c:6:19: fatal error: netdb.h: No such file or directory #include
我正在尝试编译这个程序,如 Beej 的网络编程指南 第 19 页所述。 #include #include #include #include int main() { int st
gethostbyaddr 和 netdb.h 函数在哪里实现?我只能找到它被 extern'ed 的头文件(netdb.h) 谢谢 最佳答案 它是在glibc中实现的。 顺便说一句,使用 getna
有没有办法在 ansi C 中使用 netdb.h?我想将 gcc 与 -ansi 标志一起使用,但我在这篇文章中有类似的错误 netdb.h not linking properly , 是否有可能
我正在尝试编译此代码(取自 cpp-netlib 文档): #include #include int main(int argc, char *argv[]) { using names
我有以下C代码,需要运行它。但是我在使用的库中总是出现以下错误。我尝试下载MinGW,并将其路径添加到环境变量中,但是仍然存在相同的问题。 [Error] netdb.h: No such file
我是一名优秀的程序员,十分优秀!