I understand that the config.h header file should be considered to be an internal header file. Now I wonder how to publish something that depends on config.h settings without publishing the config.h itself.
Say, I have a public function returning bool, i.e. depending on either stdbool.h or any appropriate definition of bool. It should be declared within a public header, but this header may be used in an environment without stdbool.h and maybe even without config.h or with a config.h saying nothing about the presence of bool.
I came up with an unsatisfactory solution like this:
#if HAVE_STDBOOL_H || !HAVE_CONFIG_H
# include <stdbool.h>
#endif
#if !__bool_true_false_are_defined
# if !HAVE__BOOL
typedef int _Bool;
# endif
typedef _Bool bool;
# define true (bool)1
# define false (bool)0
#endif
extern bool public_bool(void);
This should work in most standard cases but may fail in many others.
Maybe it’s obsolete taking environments without stdbool.h into consideration at all today (I don’t know), but this is just a sample of my comprehension gap concerning the autotool-thing. What is the best practice to deal with the problem in general?
In the specific case of
<stdbool.h>, we chose to avoid depending onconfig.hand to rely on the C (or C++) compiler to tell us what is OK. The code includes this header, and this header sorts out the mess. It is known to work on Mac OS X, Linux, Solaris, HP-UX, AIX, Windows. The actual header file name is notourbool.h, but I’ve mangled its header guards and comments so it looks as if that is what it is called. The comments quote the C99 standard for the licence to perform as it does. Note that it also works correctly if<stdbool.h>has already been included, and it works correctly if<stdbool.h>is included after it has been included (and getting this correct was not 100% pain-free). Defining__bool_true_false_are_definedis crucial to this inter-working. (When your code is in use by other people, you can’t mandate that they do not use<stdbool.h>themselves, either before or after your header is included.)Clearly, if you want to allow for the possibility of
HAVE_STDBOOL_H, you may, but it is not needed. You can also decide what to do ifHAVE_CONFIG_His defined, but again it is not needed.To use this, your header would contain:
The precise location of
ourbool.hinyourheader.his not critical as long as it is before you declare types, functions or (perish the thought) variables that use thebooltype.