In AVR Studio 4, has anyone else noticed that if you add the “-ffunction-sections” (that’s a double f) directive in the [All files] section of the Custom Compilation Options of a project’s configuration, the code that gcc generates is smaller than usual?
Hmmm…we use -ffunction-sections to compile the Pololu AVR C/C++ Library, so that each function we write will be in its own “section” of the object file. Then people using the library can compile with the linker option -Wl,-gc-sections, which performs garbage collection on the unused sections. This means that only the functions that are actually used in a program get compiled in to the hex file. The 3pi demo program, for example, goes from about 13k to 10k with the garbage collection option turned on.
I just tested it over here, and adding -ffunction-sections when compiling a program doesn’t change its size; this option only seems to be useful to me when compiling libraries. Do you have unused functions in your code, or are you doing something else that’s more complicated?
Complicated would be it. I’m trying to resist the urge to go to assembler. I’m still at the point of coding the service code for the sensor arrays, state machine, display management and main duty cycle, and every function is used. I’m already up to 16k of source code but compiled it drops from 7k to 5 with -ffunction-sections enabled. What might be a factor is that I’m optimising the source for size too. So, if I can identify a common set of instructions I use repeatedly and pull it out into a single function to call, I do.
Personally, before I even consider writing assembler, I get a compiler listing and look at the generated assembler code. It’s amazing how enlightening remembering how the C maps into assembler can be. Many’s the time that an advancing pointer or a structure would have saved code and time.