我想編譯這個程序,如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