0
votes

I know that for every <header.h> in the C standard library, C++ has a <cheader> header file, e.g. <stdio.h> and <cstdio>.

Every <cheader> will include its corresponding <header.h> and undefine a large list of macros which correspond to standard library functions, such as:

/*sample taken from cstdlib*/
// Get rid of those macros defined in <stdlib.h> in lieu of real functions.
#undef abort
#undef abs
#undef atexit
/* ... */

and pass the respective functions into the std namespace:

namespace std _GLIBCXX_VISIBILITY(default)
{
  _GLIBCXX_BEGIN_NAMESPACE_VERSION

  using ::div_t;
  using ::ldiv_t;

  using ::abort;
  using ::abs;
  using ::atexit;
  /* ... */
}

Looking at stdlib.h there is no trace of these macros, only the normal function prototypes. Doing a recursive search with grep at /usr/include showed me that they are not defined, only undefined at the <cheader> files. What am I missing?

My gcc version is 5.4.0, and I am in Ubuntu 16.04.3 LTS which comes bundled with Windows 10

1
They don't have to be macros (those you mention will normally not be), but they might be. This is belt-and-braces programming in your specific implementation, and is not required by the C++ Standard.user2100815

1 Answers

2
votes

Note that all you describe is specific to the particular library implementation you are observing and not common to all C or C++ libraries for all compilers.

Undefining a macro that is not defined has no effect. In order to be independent from the C library used, the C++ headers may undefine all standard symbols in case not because they are defined as macros.

For example in a free-standing environment (no OS), it is not possible to use the standard GNU C as it has operating system and POSIX dependencies. In this case an alternative C library such as Newlib may be used. However the GNU C++ library only has dependencies on the standard C library, so can be used with an alternative C library where they may be defined as macros.