3

I've just ported a STM32 microcontroller project from Keil uVision (using Keil ARM Compiler) to CooCox CoIDE (using GCC ARM Embedded compiler).

Problem is, the code size is the double size when compiled in CoIDE with GCC compared to Keil uVision.

How can this be? What can I do?

Code size in Keil: 54632b (.text) Code size in CoIDE: 100844b (.text)

GCC compiler flags:

arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -g2 -Wl,-Map=project.map -Os 
-Wl,--gc-sections -Wl,-TC:\arm-gcc-link.ld -g -o project.elf -L -lm

I am suspecting CoIDE and GCC to compile a lot of functions and files, that are present in the project, though aren't used (yet). Is it possible that it compiles whole files even if I only use 1 function out of 20 in there? (even though I have -Os)..

Jolle
  • 1,336
  • 5
  • 24
  • 36
  • 1
    In addition to @greydet's answer you can also add `--specs=nano.specs` to the linker command line. – Andy Brown Sep 22 '15 at 08:19
  • Look in the map file to see what it is – M.M Mar 09 '17 at 09:19
  • Just as a heads-up: we just had our ARM code going from 180K to 50K with arm-none-eabi-g++ v10, by moving a static method-local variable to be a class-local instead in some library. GCC couldn't optimize it properly, and pulled in a load of code to cope with a condition which couldn't happen. Optimizers are delicate magic! – SusanW Jul 28 '22 at 11:42

2 Answers2

1

Hard to say which files are really compiled/linked in your final binary from the information you give. I suppose it takes all the C files it finds on your project if you did not explicitly specified which one to compile or if you don't use your own Makefile.

But from the compiler options you give, the linker flag --gc-sections won't do much garbage if you don't have the following compiler flags: -ffunction-sections -fdata-sections. Try to add those options to strip all unused functions and data at link time.

greydet
  • 5,509
  • 3
  • 31
  • 51
0

Since the question was tagged with C++, I wonder if you would like to disable exceptions and RTTI. Those take quite a bit of code. Add -fno-exceptions -fno-rtti to linker flags.

Veletsu
  • 1
  • 2