gcc编译遇到的编译有关问题
gcc编译遇到的编译问题
使用gcc编译c文件出现的错误:
gcc -std=c99 getIP.c
getIP.c: In function ‘main’:
getIP.c:11: warning: initialization makes integer from pointer without a cast
getIP.c:12: warning: implicit declaration of function ‘gethostname’
getIP.c:18: warning: implicit declaration of function ‘getaddrinfo’
getIP.c:19: error: dereferencing pointer to incomplete type
getIP.c:21: error: dereferencing pointer to incomplete type
getIP.c:23: error: dereferencing pointer to incomplete type
换成g++编译没有任何问题:
g++ -std=c++0x getIP.c
这是为什么?
源码:
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char* argv[])
{
char host_name[128]={NULL};
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;
}
------解决思路----------------------
getIP.c:19: error: dereferencing pointer to incomplete type 这个就是
#include <netinet/in.h>
------解决思路----------------------
哦,忘了加c99的参数,我猜测getaddrinfo gethostname struct addrinfo 的定义并不在c99标准中
使用gcc编译c文件出现的错误:
gcc -std=c99 getIP.c
getIP.c: In function ‘main’:
getIP.c:11: warning: initialization makes integer from pointer without a cast
getIP.c:12: warning: implicit declaration of function ‘gethostname’
getIP.c:18: warning: implicit declaration of function ‘getaddrinfo’
getIP.c:19: error: dereferencing pointer to incomplete type
getIP.c:21: error: dereferencing pointer to incomplete type
getIP.c:23: error: dereferencing pointer to incomplete type
换成g++编译没有任何问题:
g++ -std=c++0x getIP.c
这是为什么?
源码:
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char* argv[])
{
char host_name[128]={NULL};
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;
}
------解决思路----------------------
getIP.c:19: error: dereferencing pointer to incomplete type 这个就是
#include <netinet/in.h>
------解决思路----------------------
哦,忘了加c99的参数,我猜测getaddrinfo gethostname struct addrinfo 的定义并不在c99标准中