#define FLAG headder inheritance


Id like to ask if any of you know a way of controlling the way files are included from main.cpp or some other project based file, so I have a fixed static header of libary includes that I just tell if to or not include the watchdogtimer capabilites.

I have tried to google some solutions a lot but it seems to be heavily dependent on the exact compiler, which I dont know much about. I’ll appreciate any terms or keywords I can search for too! Thanks in advance!

Just to note I use platformio.ini to include a extra_libs folder if that would be somehow connected.


#define WDTON
#include <Includes.h>

#include <stuff.h>
#ifdef WDTON
#include <avr/wdt.h>
#include <wdtManager.h>

No that should really not be the case. The defines in a programm should be completely deterministic.

You must view it like this:

  • for each compilation unit (.c or .cpp file), the file #includes some other files, which in turn include others etc… So basically a source code file is fully expanded when all includes are expanded.
  • then either a certain #define was made above some point where it was used or it hasn’t been defined. Or it has been explicitly #undef-ined.
  • global defines added by compiler flags (e.g. via GCC’s -D MACRO=VALUE directive) are always defined.

Negative example:


#ifndef SETTINGS_H  //include guard
#define SETTINGS_H

#define SETTING_X 123



#include <Arduino.h>

void setup() { 
    //some code

Since settings.h isn’t included in the code for main.cpp, the macro will not be defined when the check is done, even if it is defined in the header file.

So a #include <settings.h> must be added everywhere where such a setting is to be used.

This can be done as I just showed through a dedicated setting / configuration header file, which must taken care of to be always included in the compilation units where the the behavior shall be affected by that define.

Of course the other way is possible, too, via build_flags in the platformio.ini (docs):

build_flags =
     -D SETTTING_X=123 

The advantage of that is that this define is now globally visible in any compilation unit (unless explicitly #undef-ined. The disadvantage is that the configuratino is now made by the build-system (PlatformIO) instead of directly in code via a settings file or the likes.


Thanks, in the end im using the build flags in platformio.ini. I exactly needed a global solution like this.

1 Like