Passing defines in include files?

Every compilation unit (.cpp / .c file) receives the global defines set by the -D flags in the compiler invocation and those which are themselves included by the compilation unit

The #define DEBUG in main.cpp is not visible outside of the compilation unit.

There’s multiple ways of solving the problem:

  • creating a dedicated header file like debug_settings.h in which #define DEBUG is present or commented out; then #include <debug_settings.h> in every file in which you need to react on the macros eventually present in that file; This is the most comfortable because it’s solved purely in C++ code without needing help from the build system.
  • use global build flags to invoke the compiler with the -D DEBUG flag on every compilation unit. See Redirecting...
  • you can also tell GCC to hack in an #include <your file> in every compilation process via the -include flag. See c++ - Include header files using command line option? - Stack Overflow and again build_flags to configure it for PIO. This is rather unusual in projects though.

Doesn’t exist. This is good for variables, but not the Macro-Processor hack stuff.