Adafruit_CharacterOLED icon indicating copy to clipboard operation
Adafruit_CharacterOLED copied to clipboard

Compiler optimization flag -Ofast not working

Open baesek opened this issue 2 years ago • 1 comments

When i try to compile this lib using a compiler optimization flag other than Arduino default -Os. For example when using -Ofast I get the following error, which is if I understand it correctly a problem with the library : In function global constructors keyed to 65535_0_Device.cpp.o.6022': :(.text.startup+0x7d4): undefined reference to `Adafruit_CharacterOLED::command(unsigned char)'

`

baesek avatar May 16 '22 14:05 baesek

I found a workaround, but i dont know if it is good: I renamed the command method, which has the inline directive in its implementation, to _command and put it in the private scope of the class. Then i created a new method called command in the public space and called the _command method from there. So the inlined _command function is only used from other class methods. If i do this and call the non inlined wrapper function from outside it compiles with optimization flags. This is the excerpt from the edited implementation code.

void Adafruit_CharacterOLED::command(uint8_t value) {
  _command(value);
}
inline void Adafruit_CharacterOLED::_command(uint8_t value)
{
  send(value, LOW);
  waitForReady();
}

even Better is to put the implementation of write and command into the header file and force it to be inlined as described in this video: https://www.youtube.com/watch?v=kmHyRaiJLpQ&t=353s

resulting in this declaration of the two methods

inline virtual size_t write(uint8_t) __attribute__((always_inline));
inline void command(uint8_t) __attribute__((always_inline));

and the following definition below the class declaration:

void LCD::command(uint8_t value)
{
  send(value, LOW);
  waitForReady();
}
size_t LCD::write(uint8_t value)
{
  send(value, HIGH);
  waitForReady();
}

baesek avatar May 17 '22 07:05 baesek