What is the purpose of the HAVE_ * macros?

I am reusing some C / C ++ source files from the autotools project in a CMake project, and I see a lot of source files cluttered with lines like:

#ifdef HAVE_UNISTD_H
#include <unistd.h>  // for getpid()
#endif

I would understand the purpose of this construct if it getpid()was optional, and its call was surrounded by equivalent directives HAVE_UNISTD_H. However, without the HAVE_UNISTD_Hsource file is not compiled, complaining that it is getpid()not defined. This seems a lot more mysterious than the compiler letting me know that it was unistd.hnot found.

Of course, this is just an example. Other popular macros include HAVE_STDINT_H, HAVE_INTTYPES_Hetc., the presence of which is required to compile the source file.

Why are the guards HAVE_*turned on at all? I feel that they only bring flaws:

  • Reusing such source files requires the right header files and , for them, the correct macros are defined HAVE_*.
  • In case of an error, the developer receives a more critical message, i.e. the compiler does not report the root cause (header not found), but an auxiliary error (type / function not found).
  • The source files are slightly longer and a bit more tedious to read, i.e. #includemixed with #ifdefs.
+4
source share
2 answers

HAVE_xxx_h - POSIX . 90- , getpid(), unistd.h - , ​​, ( -) - , K & R C89 C.

. , , time.h, , sys/time.h, , , , , ! , , , Autoconf, - .

-POSIX-, . posix , , gnulib.

+9

HAVE_ * ? , :...

. , , , .

1:

#ifdef HAVE_UNISTD_H
#include <unistd.h>  // for getpid()
#endif
#ifdef HAVE_WINDoWS_H
#include <windows.h> // for GetProcessId()
#endif

1 , Windows getpid().

+1

Source: https://habr.com/ru/post/1681058/


All Articles