Building Mbed OS separately from your application

Building Mbed OS as a library

Mbed OS can be built it as a library and included within other application projects

Advantages

  • Reduces application build times
  • Reduce storage drive requirements
  • Reduces clutter (somewhat)
  • Makes it easier to create multiple applications that use the same underlying os version

This flow builds mbed-os into a .a archive and a set of header files, then copies those into another project directory for use.

It isn't perfect, but it should work. Future versions of Mbed OS tools should make this easier.

Commands for Windows

  • Create a mbed project directory for your application.

This generates necessary configuration files.

mbed new mbed-os-app-prebuilt

This last step will add a source copy of mbed-os, which we don't want, and will delete in the next step. I haven't found an alternative method that avoids this.

  • Remove the mbed OS sources

cd mbed-os-app-prebuilt

mbed remove mbed-os

  • Make an empty directory for our pre-built files to be put in later.

mkdir mbed-os

  • Now import mbed-os respository one level up from the application directory.

cd ..

mbed import https://github.com/armmbed/mbed-os

  • Change directories into the mbed-os directory

cd mbed-os

  • Compile mbed-os as a library. This generates a library output directory with header files and a library archive file.

mbed compile -m K64F -t GCC_ARM --library

  • When complete, copy the library output directory to the application directory that was created earlier.

robocopy BUILD\libraries\mbed-os\K64F\GCC_ARM ..\mbed-os-app-prebuilt\mbed-os /s /xf *.o *.d

This copies sub-directories and excludes .o and .d files.

  • Change directories into the application directory

cd ..\mbed-os-app-prebuilt

  • Add a main.cpp file for your application
  • Copy tools from the mbed-os directory that was previously built

mkdir tools

robocopy ..\mbed-os\tools /s tools

  • Copy target description file

mkdir targets

copy ..\mbed-os\targets\targets.json targets\targets.json

  • Copy linker scripts

robocopy ..\mbed-os\targets\TARGET_Freescale\TARGET_MCUXpresso_MCUS\TARGET_MCU_K64F\device\TOOLCHAIN_GCC_ARM targets /s /xf *.s

This excludes .s assembly files.

  • Now compile your application project, this time without the --library flag

mbed compile -m K64F -t GCC_ARM

You should get a result such as this. Note that the memory footprint reported is not correct when using pre-built libraries.

C:\mbed-os-app-prebuilt>mbed compile -m K64F -t GCC_ARM
[mbed] Working path "C:\mbed-os-app-prebuilt" (program)
Building project mbed-os-app-prebuilt (K64F, GCC_ARM)
Scan: mbed-os-app-prebuilt
Link: mbed-os-app-prebuilt
Elf2Bin: mbed-os-app-prebuilt
| Module            |    .text | .data |     .bss |
|-------------------|----------|-------|----------|
| [fill]            |    0(+0) | 0(+0) | 2532(+0) |
| [lib]\c.a         |  0(-220) | 0(+0) |    0(+0) |
| [lib]\misc        | 64(-116) | 0(+0) |   28(+0) |
| Subtotals         | 64(-820) | 0(+0) | 2560(+0) |
Total Static RAM memory (data + bss): 2560(+0) bytes
Total Flash memory (text + data): 64(-820) bytes

Image: .\BUILD\K64F\GCC_ARM\mbed-os-app-prebuilt.bin

Warning

This has not been extensively tested. Depending on a number of factors such as your platform or toolchain, it may not work.


Please log in to post comments.