Próbuję skompilować ten program, jak podano w Beej Guide to Network Programming na stronie 19.netdb.h nie łączące odpowiednio
#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;
}
Wśród innych błędów, widzę
../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’
Wygląda na to, że gcc nie łączy się z netdb.h
. Eclipse, IDE, którego używam do budowania tego, nie ma problemu ze znalezieniem pliku. Oto komenda kompilatora:
gcc -O0 -g3 -pedantic -Wall -c -fmessage-length=0 -ansi -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.c"
Dodanie -lnetdb
nie rozwiązuje problemu. Również ...
~> 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
Myślę, że te pliki zostały wstępnie zainstalowane na moim hoście OpenSUSE. Dlaczego gcc nie wykrywa netdb.h
? Czy wyciągam błędne wnioski?
Otrzymuję te same trzy pliki nagłówkowe jako dane wyjściowe. Widzę te błędy kompilacji: http://pastebin.com/jVVMqftA – Pieter
Więc ... to działa? Oznacza to, że nie widzisz żadnych błędów związanych z 'netdb.h', a otrzymujesz ostrzeżenie o' memset() '? To brzmi jak postęp. Ostrzeżenie 'memset()' oznacza, że musisz '#include' (pochodzi ze strony man 'memset (3)'). –
larsks
Nie widzę żadnych ostrzeżeń ani błędów, jeśli dołączę również ciąg 'string.h'. Jednak chcę, aby mój kod był zgodny z ANSI C, więc nadal dostaję te błędy ... http://pastebin.com/B21NyGQ9 – Pieter