问题描述
我不知道为什么还在说HOST_NAME_MAX是隐式的声明。
I don't know why it still says HOST_NAME_MAX is implicit declaration.
相反,我在网上搜索并执行以下操作来解决这个问题:
Instead, I searched the web and do the following to fix it:
#include <netdb.h>
和使用 MAXHOSTNAMELEN
而不是 HOST_NAME_MAX
不过,我不是很肯定它,这是一个很好的方法,它背后的原因。
however, I am not very sure it this is a good way, and the reasons behind it.
推荐答案
使用的grep
:
$ grep -rl '#define HOST_NAME_MAX' /usr/include
我们可以看到, HOST_NAME_MAX
中定义的:
We can see that HOST_NAME_MAX
is defined in:
/usr/include/bits/local_lim.h
和 local_lim.h
是 /usr/include/bits/posix1_lim.h
包括:
# grep -rl local_lim.h /usr/include
/usr/include/bits/posix1_lim.h
和 posix1_lim.h
是 limits.h中
包含仅 __ USE_POSIX
的定义是:
#ifdef __USE_POSIX
/* POSIX adds things to <limits.h>. */
# include <bits/posix1_lim.h>
#endif
所以,如果你的code如下:
So if your code looks like:
#define __USE_POSIX
#include <limits.h>
您应该有 HOST_NAME_MAX
恒可用。说了这么多,我的系统 __ USE_POSIX上
似乎默认定义。例如,下面的code:
You should have the HOST_NAME_MAX
constant available. Having said that, on my system __USE_POSIX
appears to be defined by default. For example, the following code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
int main(int argc, char **argv) {
#ifdef __USE_POSIX
printf("__USE_POSIX is defined\n");
#endif
printf("HOST_NAME_MAX: %d\n", HOST_NAME_MAX);
return 0;
}
打印:
__USE_POSIX is defined
HOST_NAME_MAX: 64
这篇关于HOST_NAME_MAX未定义后包括&LT;&limits.h中GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!