HOST_NAME_MAX undefined after include <limits.h>

天大地大妈咪最大 提交于 2019-12-09 18:10:44

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/30084116/host-name-max-undefined-after-include-limits-h

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!