首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

Linux一个extern变量的编译异常

2013-01-17 
Linux一个extern变量的编译错误我在Suse11下面用GCC4.3.4编译很小的一个程序:#includestdio.h#includen

Linux一个extern变量的编译错误
我在Suse11下面用GCC4.3.4编译很小的一个程序:


#include<stdio.h>
#include<netdb.h>
extern int h_error;
int main(void){
hostent* p1=gethostbyname("127.0.0.1");
printf("%s\n",hstrerror(h_error));
return 0;
}


但是编译报错:

g++ t.cpp
/tmp/cc3OdLYH.o: In function `main':
t.cpp:(.text+0x18): undefined reference to `h_error'
collect2: ld returned 1 exit status

问题是,man gethostbyname提示:

SYNOPSIS
       #include <netdb.h>
       extern int h_errno;

       struct hostent *gethostbyname(const char *name);
分明是存在h_error这个全局变量的啊
[解决办法]

#include<stdio.h>
#include<netdb.h>
//extern int h_error;                                                           
int main(void){
  struct hostent* p1=gethostbyname("127.0.0.1");
  printf("%s\n",hstrerror(h_errno));
  return 0;
}

热点排行