您的位置:首页 > Web前端

gcc编译出现:error: dereferencing pointer to incomplete type

2015-09-10 20:29 525 查看
使用gcc编译c文件出现如下错误:

getIP.c:14: warning: implicit declaration of function ‘gethostname’

getIP.c:20: warning: implicit declaration of function ‘getaddrinfo’

getIP.c:21: error: dereferencing pointer to incomplete type

getIP.c:23: error: dereferencing pointer to incomplete type

getIP.c:25: error: dereferencing pointer to incomplete type

编译方式:

gcc -std=c99 getIP.c

编译环境:

Red Hat Enterprise Linux Server release 6.4

源文件getIP.c:

#include <sys/socket.h>
#include <netdb.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#include <stdio.h>
#include <stdlib.h>

int main(int argc,char* argv[])
{
char host_name[128]={0};
gethostname(host_name, sizeof(host_name));
printf("host_name:%s\n",host_name);

struct addrinfo *ailist=NULL,*aip=NULL;
struct sockaddr_in *saddr;
char *addr;
int ret=getaddrinfo(host_name,"ftp",NULL,&ailist);
for(aip=ailist; aip!=NULL; aip=aip->ai_next)
{
if(aip->ai_family==AF_INET)
{
saddr=(struct sockaddr_in*)aip->ai_addr;
addr=inet_ntoa(saddr->sin_addr);
}
printf("addr:%s\n",addr);
}
getchar();
return 0;
}


以上是问题的主要描述,很奇怪的是换成g++编译没有任何问题:

g++ -std=c++0x getIP.c

在CSDN论坛中发帖寻求帮助,几度困惑和无助,但皇天不负有心人,此问题的出现是因为gcc使用了编译选项-std=c99,去掉该编译选项,顺利通过编译。 原因可能是struct addrinfo 的定义并不在c99标准中。

我们可以使用最新的c11标准,但是前提是gcc需要4.7版本之后,才真正支持c11的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: