Mbed generates so much big code size for a very small program !!!

11 Sep 2015

I am a beginner, so my question continues.

I use the following code for my F030R8 board :

#include "mbed.h"

DigitalOut myled(LED1);

int main() {
    while(1) {
        myled = 1; // LED is ON

From the Program Details of the online compiler, it shows 11.5kB of flash consumption.

I do not know where the code space is used. And is there any way of decreasing the code size ?

For comparation, I use the STM32CubeMX code generation and write a similar led on program, and it only takes about 2.6kB of code compiling by keil. This time it is reasonable.

The next step for me is digging the .map and .lst files and to find out the why. It is pretty hard for me to read these files honestly.

Can any one give some advice? Thanks.

12 Sep 2015

Do not worry. Even the simplest mbed program also includes handling code to the standard error output and error reporting (eg. Initialization UART, stdio library , text messages for run-time errors etc.).
But it can be turned off and saves a little flash.
To do this, use the the library mbed-src instead of mbed library and change the line in the file:

mbed-src/targets / hal / TARGET_STM / TARGET_STM32F0 / TARGET_NUCLEO_F030R8 / device.h



In my test of blink demo for nucleo030R8, it changed the size of the code from 12.8kB to 5.1kB! This value is already more normal for code in C ++ and object-oriented approach of the mbed, unlike the code generated with STM32CubeMX. Where is the only startup and simply initialization and operation of only those peripherals that are used in the program.
But these additional 7KB I do not think a big problem in the original Nucleo boards. This is a major problem for processors with a very small flash (just I work with such eg. f030 with 16kB flash) but mbed officially no such among platforms.

Warning. The latest versions of mbed library for Nucleo 030R8 not work properly !!!
Recent correct version is:

  • Mbed ver.101
  • Mbed-src ver.582

    After importing one of these libraries to the project folder, switch version to that works properly.
20 Feb 2017

Please, there are so many versions of mbed on the official site..I am lost there. Would you please let me know where or the link of the mbed-src .. I guess it should be version 2.0? I appreciate your help.

21 Nov 2017

The thread is quite old, but for future readers - how to import that library:

  • right-click @ project name
  • select Import Library...
  • select From Import Wizard...
  • in the search field of the new window (top right) enter mbed-src and click Search
  • double-click the desired line, usually the one with the largest number of imports or even better the most recent one like mbed-dev.

And please don't forget to delete the bulky library mbed from your project.

22 Nov 2017

Thank you for your reply.

24 Nov 2017

I would add to not use mbed-src anymore. It is deprecated. Use definitely mbed-dev.