概述
使用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:%sn",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:%sn",addr);
}
getchar();
return 0;
}
以上是问题的主要描述,很奇怪的是换成g++编译没有任何问题:
g++ -std=c++0x getIP.c
在CSDN论坛中发帖寻求帮助,几度困惑和无助,但皇天不负有心人,此问题的出现是因为gcc使用了编译选项-std=c99,去掉该编译选项,顺利通过编译。 原因可能是struct addrinfo 的定义并不在c99标准中。
我们可以使用最新的c11标准,但是前提是gcc需要4.7版本之后,才真正支持c11的。
最后
以上就是多情冥王星为你收集整理的gcc编译出现:error: dereferencing pointer to incomplete type的全部内容,希望文章能够帮你解决gcc编译出现:error: dereferencing pointer to incomplete type所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复