读书人

Linux一个extern变量的编译异常

发布时间: 2013-01-17 10:28:54 作者: rapoo

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;
}

读书人网 >C++

热点推荐