Practical Guide to Bare Metal C++
  • Introduction
  • Audience
  • C++ Popularity
  • Benefits of C++
  • Contents of This Book
  • Contribution
  • Know Your Compiler Output
    • Test Applications
    • Get Simple Application Compiled
    • Dynamic Memory Allocation
    • Exceptions
    • RTTI
    • Removing Standard Library and C++ Runtime
    • Static Objects
    • Abstract Classes
    • Templates
    • Tag Dispatching
  • Basic Needs
    • Assertion
    • Callback
    • Data Serialisation
    • Static (Fixed Size) Queue
  • Basic Concepts
    • Event Loop
    • Device-Driver-Component
  • Peripherals
    • Timer
    • UART
    • GPIO
    • I2C
    • SPI
    • Other
Powered by GitBook
On this page

Was this helpful?

  1. Know Your Compiler Output

Removing Standard Library and C++ Runtime

PreviousRTTINextStatic Objects

Last updated 5 years ago

Was this helpful?

Due to platform RAM/ROM limitations it may be required to exclude not just support for exceptions and RTTI (compiling with -fno-exceptions -fno-unwind-tables -fno-rtti), but for dynamic memory allocation too. The latter includes passing -nostdlib option to the compiler. In case when standard library is excluded, there is no startup code help provided by the compiler, the developer will have to implement all the startup stages:

  • updating the interrupt vector table

  • setting up correct stack pointers for all the modes of execution

  • zeroing .bss section

  • calling initialisation functions for global objects

  • calling “main” function.

is an example of such startup code.

There also may be a need to provide an implementation of some functions or definition of some global symbols. For example, if algorithm is used to copy multiple objects from place to place, the compiler might decide to use function provided by the standard library, and as the result the build process will fail with “undefined reference” error. The same way, usage of algorithm may require function. Be ready to implement them when needed.

Another example is having call to function with , , etc. There will be a need to define these placeholders as global symbols:

#include <functional>
namespace std 
{ 
namespace placeholders 
{ 

decltype(std::placeholders::_1) _1; 
decltype(std::placeholders::_2) _2; 
decltype(std::placeholders::_3) _3; 
decltype(std::placeholders::_4) _4; 

}  // namespace placeholders 
}  // namespace std

Even if there is a need for the standard library in the product being developed, it may be a good exercise as well as good debugging technique to temporarily exclude it from the compilation. The compilation will probably fail in the linking stage. The list of missing symbols and/or functions will provide a good indication of what missing functionality is provided by the library. The developer may notice that some components still require exceptions handling, for example, resulting int the binary image being too big.

Here
std::copy
memcpy
std::fill
memset
std::bind
std::placeholders::_1
std::placeholders::_2