Program Sizes Question?

I just recently purchased a 3pi robot and so far it has been a lot of fun to learning about the world of small microcontrollers. It is quite a bit different from creating a program for an average PC and certainly presents a challenge when working with limited memory resources and microcontroller clock cycles. This leads into the question for this post.

When I create a new project in AVR Studio 4 and copy and paste code from the examples in the Pololu AVR Library, the compiled code in my new project is about 10% larger then the example code. Even the .hex files are larger by about 10 kilobytes. I know 10kb is not that much of a difference in the world of PCs, but when you are limited to a total of 32kb, 10kb is a big difference. I tried going through the configuration options for the project and matched each projects settings, but they are still larger. Here is the compiler output for each project:

(The code used is from the 3pi-linefollower-pid example: Pololu AVR Library\libpololu-avr\examples\atmega328p\3pi-linefollower-pid)

My new project output:

Build started 14.3.2010 at 08:37:21
avr-gcc  -mmcu=atmega328p -Wall -gdwarf-2 -Os -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT Main.o -MF dep/Main.o.d  -c  ../Main.c
avr-gcc -mmcu=atmega328p -Wl,-Map=TestLineFollow.map Main.o    -lpololu_atmega328p  -o TestLineFollow.elf
avr-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature  TestLineFollow.elf TestLineFollow.hex
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O ihex TestLineFollow.elf TestLineFollow.eep || exit 0
avr-objdump -h -S TestLineFollow.elf > TestLineFollow.lss

AVR Memory Usage
----------------
Device: atmega328p

Program:   12280 bytes (37.5% Full)
(.text + .data + .bootloader)

Data:        133 bytes (6.5% Full)
(.data + .bss + .noinit)

Build succeeded with 0 Warnings...

Pololu AVR Library example output:

Build started 14.3.2010 at 08:36:24
avr-gcc  -mmcu=atmega328p -Wall -gdwarf-2 -std=gnu99 -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT test.o -MF dep/test.o.d  -c  ../test.c
avr-gcc -mmcu=atmega328p -Wl,-gc-sections -Wl,-Map=test.map test.o    -lpololu_atmega328p  -o test.elf
avr-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature  test.elf test.hex
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O ihex test.elf test.eep || exit 0
avr-objdump -h -S test.elf > test.lss

AVR Memory Usage
----------------
Device: atmega328p

Program:    9182 bytes (28.0% Full)
(.text + .data + .bootloader)

Data:        127 bytes (6.2% Full)
(.data + .bss + .noinit)

Build succeeded with 0 Warnings...

If you look at the AVR Memory Usage output / Program, it is different by ~2000kb. I was wondering, does anyone know why my program size is bigger, even though the amount of code is the same? Could it be possible when I create a new project, AVR Studio 4 is automatically importing an external library?

I think that your questions will be answered by our discussion of Using the Pololu AVR Library for your own projects. My guess would be the missing -gc-sections option.

-Paul

Hello.

You should also look under the project’s configuration options to make sure you’re compiler optimization is -Os (optimize for size). Without using this optimization, your compiled code size can be significantly larger.

- Ben

Thanks for the help, Paul and Ben. Yep I was missing the -Wl,-gc-sections linker options for my projects. I thought I had downloaded all of the documentation for the 3pi, but it looks like I missed some. Thank you again for pointing me in the right direction :smiley: