Linux一个extern变量的编译错误

sfevne 2012-12-12 03:53:14
我在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这个全局变量的啊
...全文
96 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
swordtan 2012-12-12
  • 打赏
  • 举报
回复
h_errno 你写错了
图灵狗 2012-12-12
  • 打赏
  • 举报
回复

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

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧