lexus2k / ssd1306

Driver for SSD1306, SSD1331, SSD1351, IL9163, ILI9341, ST7735, PCD8544, Nokia 5110 displays running on Arduino/ESP32/Linux (Rasperry) platforms
MIT License
651 stars 125 forks source link

nano_gfx_types.h defines macros which pollutes the global namespace and cause compile errors #146

Open fergald opened 2 years ago

fergald commented 2 years ago

The following code fails to compile when the #include is present

#include "ssd1306.h"

void setup() {
  Serial.println(std::numeric_limits<int16_t>::max());

}

void loop() {
}

The error is

sketch_may15a:4:52: error: macro "max" requires 2 arguments, but only 1 given
    4 |   Serial.println(std::numeric_limits<int16_t>::max());
      |                                                    ^
In file included from /home/fergal/Arduino/libraries/ssd1306/src/ssd1306.h:31,
                 from /home/fergal/Arduino/sketch_may15a/sketch_may15a.ino:1:
/home/fergal/Arduino/libraries/ssd1306/src/nano_gfx_types.h:40: note: macro "max" defined here
   40 | #define max(a,b) ((a)>(b)?(a):(b))
      | 
exit status 1
macro "max" requires 2 arguments, but only 1 given

The code above is just the simplest repro case. I ran into this in real life because esp8266/StreamString.h contains the following

    virtual int availableForWrite() override
    {
        return std::numeric_limits<int16_t>::max();
    }

In general you shouldn't define macros in .h files that will end up included by users of the library unless they are intended to be used by those users (and then naming them becomes tricky).