10

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.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
yuan
  • 317
  • 1
  • 4
  • 12
  • `HOST_NAME_MAX` is not defined in ``. Why did you expect it to be? – Keith Thompson May 06 '15 at 17:54
  • 1
    @Keith So where is it defined ? I've checked this and it seems defined [link](http://pubs.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html) – yuan May 06 '15 at 17:55
  • 1
    Sorry, my mistake. It *is* defined in ``, but not by default. (It's specified by POSIX, not by C). There's a way to specify that you want `` to define it, but I don't remember it off the top of my head. – Keith Thompson May 06 '15 at 17:58
  • @KeithThompson thanks ! I will try to search and also see what others come up with .... – yuan May 06 '15 at 18:00

1 Answers1

14

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
larsks
  • 277,717
  • 41
  • 399
  • 399
  • I can see that it is implemented like you posted, however from http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_02.html I can see that _POSIX_C_SOURCE should be used instead of __USE_POSIX according to standard. This won't work (as it is not coded like this). Do you know why it is not coded with _POSIX_C_SOURCE? – Davidius Oct 11 '17 at 08:11
  • 1
    After doing further investigation since it was not working for me while looking inside "posix1_lim.h" I found out that some stuff has been changed **HOST_NAME_MAX** now is **_POSIX_HOST_NAME_MAX** **LOGIN_NAME_MAX** now is **_POSIX_LOGIN_NAME_MAX** https://pastebin.com/kFmAbWcJ – Nfagie Yansaneh Oct 13 '17 at 02:05