0

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
  • 2
    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. Commented Nov 28, 2017 at 22:41

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah I think I got it. So the C++ implementation I have is decoupled from the C implementation I also have. Those undefines are a precaution against the case that a C library used as a dependency had those standard functions defined as macros, which is already rare.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.