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.hin which#define DEBUGis 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 DEBUGflag on every compilation unit. See Redirecting... - you can also tell GCC to hack in an
#include <your file>in every compilation process via the-includeflag. See c++ - Include header files using command line option? - Stack Overflow and againbuild_flagsto 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.