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>
and use MAXHOSTNAMELEN
instead of HOST_NAME_MAX
however, I am not very sure it this is a good way, and the reasons behind it.
Using grep
:
$ grep -rl '#define HOST_NAME_MAX' /usr/include
We can see that HOST_NAME_MAX
is defined in:
/usr/include/bits/local_lim.h
And local_lim.h
is included by /usr/include/bits/posix1_lim.h
:
# grep -rl local_lim.h /usr/include
/usr/include/bits/posix1_lim.h
And posix1_lim.h
is included by limits.h
only if __USE_POSIX
is defined:
#ifdef __USE_POSIX
/* POSIX adds things to <limits.h>. */
# include <bits/posix1_lim.h>
#endif
So if your code looks like:
#define __USE_POSIX
#include <limits.h>
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;
}
Prints:
__USE_POSIX is defined
HOST_NAME_MAX: 64