"undefined reference" errors when the code base became too big [Resolved]

I have a basic project targeting Arduino Mega board [1] that seems to stop compiling once the source code reaches a certain “size”.

In the following function [2],

    bool hasAnotherLightPressed(int const led) {
        for (int i = 0; i < LED_COUNT; i += 1) {
            if (i != led && board::isPressed(i)) {
                // Serial.print("<? ");
                Serial.println(i);
                return true;
            }
        }
        return false;
    }

if I uncomment the line Serial.println(i), the compilation starts throwing errors like the following:

/tmp/ccrn2Vp1.ltrans1.ltrans.o:(.rodata+0x30): undefined reference to ‘Game<snake::GameState>::initState(snake::GameState&)’
/tmp/ccroduzk.ltrans1.ltrans.o:(.rodata+0x32): undefined reference to ‘Game<snake::GameState>::copy(snake::GameState const&, snake::GameState&)’

Commenting back the line resolves the compilation error.
I can uncomment the line and go around in the code removing other Serial.print, or bodies of some methods, and the code starts compiling again.

After a successful compilation, I got the following report:

RAM : [ ] 4.9% (used 404 bytes from 8192 bytes)
Flash: [ ] 3.0% (used 7590 bytes from 253952 bytes)

It does not seem that the whole program is that big, but still, the issue seems related to the amount of code I have.
Finally, I reproduce the issue with the latest PIO on two distinct machines (one Mac and a Linux PC).
Sorry for not having identified a much simpler case to reproduce the issue.

Note that the same error initially appear in the project, without any Serial, after I added a new class.

Thanks for any lead on that issue.

[1] https://gitlab.com/kineolyan/guak-a-mole/-/tree/compilation-error/guak-a-box
[2] https://gitlab.com/kineolyan/guak-a-mole/-/blob/compilation-error/guak-a-box/src/simon.cpp#L109-121

It’s a compiler bug. 100% reproducable on the default-used avr-gcc 5.4.0.

To upgrade the compiler, set the platformio.ini to

[env:megaatmega2560]
platform = atmelavr
board = megaatmega2560
framework = arduino
platform_packages =
  ; use GCC AVR 7.3.0+
  toolchain-atmelavr@>=1.70300.0

[platformio]
description = Super box inspired from Whak-a-mole, with additional games on the same board

PS: We already have an issue open at Using toolchain-atmelavr v5.4.0 vs v7.3.0 · Issue #224 · platformio/platform-atmelavr · GitHub to default to the newer gcc version.

Many thanks @maxgerhardt. That’s amazing. You made my weekend better as I can now continue working on my project :slight_smile: