I'm developing the program for the AVR microcontroller in C++. I have created a class that contains 8 methods(with constructors) but used only 3. Class code:

class Pin {
    Pin(PinId pin_id);
    Pin(Port port, uint8_t pin);

    void SetDirection(Direction direction);
    Direction GetDirection() const;

    void Write(Signal signal);
    Signal Read() const;

    void Toggle();

I expect that in the build file unused methods won't be, but it will. I use the Ghidra disassembler to disassemble the build file. Pin(Port port, uint8_t pin), SetDirection and Toggle are used only (simple LED flasher). But there are all of these methods in the build file and there are no references to these methods. So, why compiler/optimizer/linker doesn't remove these methods from the build file? I compile the program with -Os optimization parameter and Release CMake configuration. For example, the Write method in the Ghidra: enter image description here But the Write method isn't used in my code (and in other class' methods too). How can I optimize it? Thanks in advance!

  • \$\begingroup\$ what file exactly did you analyze? usually, at the end of everything, you might be getting e.g. an ELF file that still contains symbol names, but these would be discarded upon loading the ELF using a debugger into the device's RAM, or during a step that gives you the plain binary. Of course, if you use C++ and Reflection, or extensive virtual inheritance, you can't throw away any symbols early. \$\endgroup\$ Jan 2, 2021 at 14:04
  • \$\begingroup\$ @MarcusMüller, I analyzed the ELF file compiled by avr-g++. I don't use virtual functions. Output of avr-size is Program: 682 bytes and Data: 0 bytes \$\endgroup\$ Jan 2, 2021 at 14:11
  • \$\begingroup\$ @MarcusMüller, I just removed a few unused methods from the class and the output from avr-size decreased from 682 bytes to 548. \$\endgroup\$ Jan 2, 2021 at 14:13
  • 2
    \$\begingroup\$ to see in more detail the size of functions etc, have it output a .map file \$\endgroup\$
    – Pete W
    Jan 2, 2021 at 14:29
  • 1
    \$\begingroup\$ @PeteW, yeah, it worked, code size reduced to 472 bytes, thanks. You can post the answer if you want \$\endgroup\$ Jan 2, 2021 at 14:56

1 Answer 1


If using GCC (AVR-GCC), the following options can be helpful


-fdata-sections -ffunction-sections



Additional explanation:

The above options work together as follows

-ffunction-sections and -fdata-sections -- these put each function or data item into its own section in the output file (i.e. object file coming out of compiler). At this stage it's too soon to say whether we can garbage collect anything, only when linking will there be enough information to make that decision, hence the linker option below

-Wl,--gc-sections -- figures out if there are "sections" that are never used and removes them ("garbage-collection")...

Reference: the documentation

Also, examining the .map file is a way to deduce how much individual functions/methods or static data items are contributing to the total size

  • \$\begingroup\$ Upvoted, but a short explanation what those compiler options actually do, would be nice to complete this answer. \$\endgroup\$
    – Grebu
    Jan 4, 2021 at 12:25
  • \$\begingroup\$ @Grebu - updated \$\endgroup\$
    – Pete W
    Jan 4, 2021 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.