【发布时间】:2015-01-27 12:03:10
【问题描述】:
我在代码中使用gethostbyname_r() 函数,编译时出现以下错误。
gethostname.cpp:17: 错误:'gethostbyname_r' 未在此范围内声明
我的代码:
#include<stdio.h>
#include<netdb.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/file.h>
int main()
{
struct hostent hostbuf;
struct hostent *hp = NULL;
char* hostent_buff;
int herr = 0;
int hres = 0;
hres = gethostbyname_r("domain name", &hostbuf,
hostent_buff, 1024, &hp, &herr);
if (NULL == hp)
{
return -1;
}
return 0;
}
谁能帮助我? 我可以在 Linux 和 Solaris 上使用相同的函数 gethostbyname_r() 吗?
【问题讨论】:
-
只是好奇,为什么你的文件名为
gethostname.cpp?请问你用的是什么编译器? -
另请注意,
gethostbyname_r是 GNU 扩展,而不是标准的C库函数。 -
另请注意,现代代码应使用
getnameinfo和getaddrinfo代替 -
错误信息还有更多内容吗?也许是签名不匹配?根据我的手册页,你有正确的#includes。
-
@Sourav gethostname.c 也无法获取 ld: Unsatisfied symbol "gethostbyname_r" in file /var/tmp//ccLwNIet.o
标签: c linux networking