cancel
Showing results for 
Search instead for 
Did you mean: 

C++ compiler options.

CPetr.1
Associate II

 Hi.

I thought this question is more suited to education.

I have been working on a STM32F091 C++ project just to get an idea code size, and I noticed that the size of the binary is huge even when optimisation is set to (-Os). I think its because the compiler is bringing in everything, even when certain functions, and section are not being used, but! I’m not sure.

Generally speaking, is there a way if streamlining my project to cut down on code?

Thanks

cp

5 REPLIES 5
TDK
Guru

> I think its because the compiler is bringing in everything, even when certain functions, and section are not being used, but! I’m not sure.

You can open up the map file and figure out what's taking so much space. Ensure your linker discards unused code (-WL,--gc-sections)

> Generally speaking, is there a way if streamlining my project to cut down on code?

Remove unnecessary code, don't duplicate code.

Use a reduced library such as nano (--specs).

Avoid using dynamic memory allocation (malloc/free).

The -Os optimization flag should be about the best you can do for size from a compiler standpoint. Might get more with link time optimization (-flto).

If you feel a post has answered your question, please click "Accept as Solution".
CPetr.1
Associate II

Thanks for your answer TDK.

Where do I place (-WL,--gc-sections) and  (--specs) in the cubeIDE settings.

TDK
Guru

In project settings.

0693W00000DmOk1QAF.png0693W00000DmOkBQAV.png

If you feel a post has answered your question, please click "Accept as Solution".
CPetr.1
Associate II

ok, I checked my settings, and that's the way they are set up already. This code I'm using compiles for a mega2560 AVR at just under 128K, but as it is, ported to STM32F091, it generates 241k of code. That a big difference.

CPetr.1
Associate II

Just to complete this thread, one of the files contained an #include <iostream> statement.

When I removed this, the code size came down to 88K, which seems about right for this project size.

cheers