[Solved!] Any way to remove unused virtual functions from final image?

Something that worked!

  • Define a replacement function in your .INO file (or any other file that will be linked ahead of the library that has the function you want to whack)
  • add '-Wl,--allow-multiple-definition' to the c.elf flags. You can add this in platform.txt, or use project specific options if your IDE allows it.

For my project, normal build (using a MEGA this time):

Program size: 24,088 bytes (used 9% of a 253,952 byte maximum) (3.84 secs)
Minimum Memory Usage: 1404 bytes (17% of a 8192 byte maximum)

With this option enabled, and the following in my .INO file:

int EthernetClient::connect(const char*, uint16_t)
{
 return 0;
}

the build returns this now:

Program size: 20,858 bytes (used 8% of a 253,952 byte maximum) (3.38 secs)
Minimum Memory Usage: 1356 bytes (17% of a 8192 byte maximum)

So, not only a lot less code space used, but some RAM savings as well.

For robustness, the replacement function should probably do something that will be easily noticeable during development, because if you see it, you need the original function.