Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. Use link time optimizations to save flash / program storage space (Sketch too big error)

Use link time optimizations to save flash / program storage space (Sketch too big error)

Scheduled Pinned Locked Moved Development
sizesketch too bigprogram storage spaceflash
10 Posts 2 Posters 2.7k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mfalkviddM Online
    mfalkviddM Online
    mfalkvidd
    Mod
    wrote on last edited by mfalkvidd
    #1

    A friend of mine who is way more savvy in c/c++ programming than me mentioned that enabling link time optimizations (LTO) might save some flash space. I decided to do some research and it turns out that LTO is actually supported by the Arduino IDE toolchain, but it is not enabled by default.

    I did some digging and figured out how to activate it. These are the results:
    GatewaySerial on Atmega328 without LTO:

    Sketch uses 15,170 bytes (49%) of program storage space. Maximum is 30,720 bytes.
    Global variables use 711 bytes (34%) of dynamic memory, leaving 1,337 bytes for local variables. Maximum is 2,048 bytes.
    

    GatewaySerial on Atmega328 with LTO:

    Sketch uses 14,050 bytes (45%) of program storage space. Maximum is 30,720 bytes.
    Global variables use 712 bytes (34%) of dynamic memory, leaving 1,336 bytes for local variables. Maximum is 2,048 bytes.
    

    1,120 bytes flash saved (7.4%)!

    SecureActuator on Atmega328 without LTO:

    Sketch uses 23,572 bytes (76%) of program storage space. Maximum is 30,720 bytes.
    Global variables use 1,258 bytes (61%) of dynamic memory, leaving 790 bytes for local variables. Maximum is 2,048 bytes.
    

    SecureActuator on Atmega328 with LTO:

    Sketch uses 21,242 bytes (69%) of program storage space. Maximum is 30,720 bytes.
    Global variables use 1,241 bytes (60%) of dynamic memory, leaving 807 bytes for local variables. Maximum is 2,048 bytes.
    

    2,330 bytes flash saved (9.9%)!

    Pretty sweet!

    Here is how to enable it. Shut down the Arduino IDE. Put the following lines:

    compiler.c.extra_flags=-flto -fno-fat-lto-objects
    compiler.c.elf.extra_flags=-flto -fuse-linker-plugin
    compiler.S.extra_flags=-flto
    compiler.cpp.extra_flags=-flto
    compiler.ar.cmd=avr-gcc-ar
    

    into a new file at "C:\Program Files (x86)\Arduino\hardware\arduino\avr\platform.local.txt" (or the corresponding place on your OSX/Linux machine)

    This might help people trying to fit code into attiny85 or similar mcus with small flash size, or people who try to fit a lot of libraries and/or functionality (for example MY_DEBUG and MY_SIGNING_SOFT at the same time), getting the dreaded "Sketch too big; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing it." error.

    Caveat: There might be some side-effects of the optimizations. I don't kow what they might be or how common they would be, so please try this and report your results.

    1 Reply Last reply
    4
    • gohanG Offline
      gohanG Offline
      gohan
      Mod
      wrote on last edited by
      #2

      I'll try to experiment with it. Thanks

      1 Reply Last reply
      0
      • gohanG Offline
        gohanG Offline
        gohan
        Mod
        wrote on last edited by
        #3

        I didn't see any change in compile size... maybe it didn't like the new created file

        mfalkviddM 1 Reply Last reply
        0
        • gohanG gohan

          I didn't see any change in compile size... maybe it didn't like the new created file

          mfalkviddM Online
          mfalkviddM Online
          mfalkvidd
          Mod
          wrote on last edited by mfalkvidd
          #4

          @gohan you did have the IDE closed when adding the file?
          Which board are you using? The change documented above will only affect Arduino avr boards. Boards using other board definitions will not be affected.

          1 Reply Last reply
          0
          • gohanG Offline
            gohanG Offline
            gohan
            Mod
            wrote on last edited by
            #5

            I'm using the usual pro mini 3.3v. Yes it was closed and the file is there.

            mfalkviddM 1 Reply Last reply
            0
            • gohanG gohan

              I'm using the usual pro mini 3.3v. Yes it was closed and the file is there.

              mfalkviddM Online
              mfalkviddM Online
              mfalkvidd
              Mod
              wrote on last edited by
              #6

              @gohan strange. Which version of the Arduino Avr boards are you using? I am using 1.6.20. I think the minimum version is 1.6.10.

              1 Reply Last reply
              0
              • mfalkviddM Online
                mfalkviddM Online
                mfalkvidd
                Mod
                wrote on last edited by
                #7

                I think I read somewhere that the location of the platform.local.txt file was moved in some version of the Arduino IDE. I am using 1.6.8. Is there a platform.txt file in the folder where you put the local file on your computer?

                1 Reply Last reply
                0
                • gohanG Offline
                  gohanG Offline
                  gohan
                  Mod
                  wrote on last edited by
                  #8

                  it is in the same folder as platform.txt

                  1 Reply Last reply
                  0
                  • gohanG Offline
                    gohanG Offline
                    gohan
                    Mod
                    wrote on last edited by
                    #9

                    I am still lost with this... I can't get it to work... :cry:

                    1 Reply Last reply
                    0
                    • mfalkviddM Online
                      mfalkviddM Online
                      mfalkvidd
                      Mod
                      wrote on last edited by mfalkvidd
                      #10

                      Update: LTO is enabled by default for avr core/board version 1.6.12 and newer.

                      1 Reply Last reply
                      1
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      18

                      Online

                      11.7k

                      Users

                      11.2k

                      Topics

                      113.0k

                      Posts


                      Copyright 2019 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • MySensors
                      • OpenHardware.io
                      • Categories
                      • Recent
                      • Tags
                      • Popular