error: ‘struct hostent’ has no member named ‘h_addr’


這是編譯選項造成的。

例如,如下的編譯選項就可能出現問題:

> gcc -pedantic -Os -c client.c -o client.o -std=c99


在netdb.h文件中,可以找到hostent的定義:

/* Description of data base entry for a single host.  */
struct hostent
{
  char *h_name;			/* Official name of host.  */
  char **h_aliases;		/* Alias list.  */
  int h_addrtype;		/* Host address type.  */
  int h_length;			/* Length of address.  */
  char **h_addr_list;		/* List of addresses from name server.  */
#if defined __USE_MISC || defined __USE_GNU
# define	h_addr	h_addr_list[0] /* Address, for backward compatibility.*/
#endif
};


解決辦法,加上編譯選項即可:

> gcc -pedantic -Os -c client.c -o client.o -std=c99 -D_GNU_SOURCE



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章