Skip to main content
CPetr.1
Associate II
September 5, 2021
Question

C++ compiler options.

  • September 5, 2021
  • 5 replies
  • 2723 views

 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

This topic has been closed for replies.

5 replies

TDK
Super User
September 5, 2021

> 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
CPetr.1Author
Associate II
September 7, 2021

Thanks for your answer TDK.

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

TDK
Super User
September 7, 2021

In project settings.

0693W00000DmOk1QAF.png0693W00000DmOkBQAV.png

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

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
CPetr.1Author
Associate II
September 11, 2021

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