我想编译这个程序,如Beej指南中引用到网络第19页netdb.h中未正确连接
#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
?或者我得出错误的结论?
我得到与输出相同的三个头文件。我看到这些编译错误:http://pastebin.com/jVVMqftA – Pieter
所以...它的工作?也就是说,你没有看到与'netdb.h'有关的任何错误,而你只是收到关于'memset()'的警告?这听起来像是进步。 'memset()'警告意味着你需要'#include'(这来自'memset(3)'手册页)。 –
larsks
如果我还包含'string.h',我不会看到任何警告或错误。但是,我希望我的代码符合ANSI C标准,因此我仍然收到这些错误... http://pastebin.com/B21NyGQ9 – Pieter