cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with optimal code size

NPham.2
Associate II

Hello,

I use STM32IDE to programmer for STM32F030K6, when i call the function in the main function or interrupt function, it consumes a lot of Flash.0693W00000HoFjnQAF.png0693W00000HoFjxQAF.pngI have already changed the optimization option. But it doesn't work.

0693W00000HoFkCQAV.pngI try again with Keil C and this error does not occur, the code size is well optimized.

0693W00000HoFkMQAV.pngCan you guys tell me what I did wrong? Thanks you all!

13 REPLIES 13
S.Ma
Principal

To investigate in less than 1 hour, compare keil map file with gcc map file. You will find out the biggest object file size difference.and zoom in. When you call a function from main, the linker will recursively find thr function tree and add them to memory. If you use function pointerd, linkers are less guessing hence optimizing. Keil and IAR are morr efficient than gcc. Dig and compare map file. Also check the project compile option or global tags in the project config, maybe assert options, etc...

S.Ma
Principal

Check also if using float or double that if the fpu is present, it uses it instead of a sw lib that emulates it.

If you are serious, the best solution is to eliminate those functions and use, for example, nanoprintf. It's small, configurable, reentrant and it uses single (32-bit) float for floating point arithmetic. LwPRINTF is another decent alternative. It's also configurable and reentrant, but, compared to nanoprintf, is slightly bigger, better tailored for RTOS (integrated mutex support) and uses double (64-bit) float for floating point arithmetic.

And about those 2 KB of initialization data... Basically you specify a -nostartfiles option for the linker and comment out a __libc_init_array call from your startup file. But, before doing that, read this discussion.

Forgot one more feature. Set compiler optimization to Os and also turn on LTO (link-time optimization) in linker settings. That's the smallest code one can get.