ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
c_cpp_compatibility.h File Reference

Fix macro conflicts between PostgreSQL headers and the C++ STL/Boost. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Detailed Description

Fix macro conflicts between PostgreSQL headers and the C++ STL/Boost.

Two families of PostgreSQL macros break C++ headers included after them:

  • When PostgreSQL is built without --enable-nls, its headers redefine gettext, ngettext, dgettext, and dngettext as no-op macros. Several STL and Boost headers later include <libintl.h>, which tries to declare the real function prototypes for those same names. The resulting macro/declaration clash causes compilation errors.
  • port.h replaces the printf family (snprintf, sprintf…) with pg_* macros for output consistency. Any later-included C++ header that calls the std::-qualified functions then breaks – e.g. boost/assert/source_location.hpp (Boost >= 1.79) calls std::snprintf, which the macro turns into the non-existent std::pg_snprintf.

This header must be included in every translation unit that mixes PostgreSQL C headers with C++ STL or Boost headers, after the PostgreSQL headers. It undefines the offending macros so subsequent STL/Boost headers parse cleanly. C++ code does not rely on pg_snprintf semantics (such as m), so losing the replacement is harmless. provsql_utils.h includes it automatically when compiled as C++, so most translation units are covered transitively.

Deliberately no include guard: undefining is idempotent, and a guard would make a second inclusion a no-op – leaving the macros defined when the first inclusion happened before the PostgreSQL headers.

The cleanest long-term fix for the gettext clash is to build PostgreSQL with --enable-nls, but that is not always under the extension author's control.

Definition in file c_cpp_compatibility.h.