Weird (erroneous) compiler and linker behaviour

#if BOARD_ID == 1 || BOARD_ID == 2 || BOARD_ID == 3 || BOARD_ID == 102
#include <SSD1306_Text_Basic.h>

Look at the code segment above. We have a “#if BOARD_ID == 1…” block, which I know to be false. As a result I expect all the code within to be compiled out.

This is basic, and true. I can type anything withing that block and it would be completely ignored.

However this does not happen exactly like that with Arduino IDE.

The compiler attempts to include the “SSD1306_Text_Basic.h” file in its calculations. How? Well, the file is a library in the libraries directory. The compiler uses that path and compiles present files inside this directory and extracts symbols refered to, accidentally, by other modules. Of course then you get all sorts of unexpected and incorrect results.

This must be an artifact of how the IDE handles librabries.

However why is the compiler adding into its compilation routing a path which has been #if’ed out?

Let’s play:

#if BOARD_ID == 1 || BOARD_ID == 2 || BOARD_ID == 3 || BOARD_ID == 102
#include <SSD1306_Text Mickey Mouse _Basic.h>

Code now compiles fine. Obviously “SSD1306_Text Mickey Mouse _Basic.h” does not exist, and so it gets ignored. As it should in the first place since it’s within a “#if 0” block.

Why is the compiler even LOOKING inside a “#if 0” block is unreal, there must be a preprocessing phase, and the writers of that preprocessor have not obeyed fundamental C rules, a “#if 0” is pretty basic.