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:
以上是问题的主要描述,很奇怪的是换成g++编译没有任何问题:
g++ -std=c++0x getIP.c
在CSDN论坛中发帖寻求帮助,几度困惑和无助,但皇天不负有心人,此问题的出现是因为gcc使用了编译选项-std=c99,去掉该编译选项,顺利通过编译。 原因可能是struct addrinfo 的定义并不在c99标准中。
我们可以使用最新的c11标准,但是前提是gcc需要4.7版本之后,才真正支持c11的。
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的。
相关文章推荐
- gcc编译出现:error: dereferencing pointer to incomplete type
- JSON解析----个人学习笔记
- jquery && ajax check username
- js 调试
- HTML 简介
- 剑指Offer系列---(17)调整数组顺序使奇数位于偶数前面
- Javascript学习笔记
- Javascript学习笔记
- jQuery 的选择器
- HTML中id、name、class 区别
- jQuery的简单了解
- CSS布局
- js动态时钟
- 如何用 OneAPM 优化你的 Node.js 应用?
- 如何用 OneAPM 优化你的 Node.js 应用?
- [Bootstrap]7天深入Bootstrap(4)CSS组件
- js倒计时
- js获取元素经过计算的css样式
- 《剑指offer》二进制中1的个数
- javascript中闭包