2

I mainly develop firmware for ARM Cortex M using gcc and I am migrating from C to C++ and even though I am overall happy with the transition, there are some things that I just can't get right.

Currently I am working on a project with FreeRTOS and I am trying to use emio (https://github.com/Viatorus/emio), that is a bit like fmt but a little bit more lightweight.

When I compile the whole project with -O2 or even with -Og stack usage is reasonable, but even though with plain C -Og was good for debugging, with C++ I can't put breakpoints on most templated classes/methods as they are apparently being inlined/optimized, so I have to go down to -O0 to properly debug when needed.

Unfortunately -O0 has quite an issue with emio; any task that uses formatting (that I use for logging) needed like 6K of stack to avoid overflow, whereas higher optimization can be under 1.5K.

Is there any way to make a header only library be compiled with -O2 in all sources that use the library, so I can have good debugging without having to artificially change all stack levels? Or is there any other tip or suggestion on how more seasoned developers work with this kind of issues?

Since there is a sole header file to include, I tried putting a pragma to force all the emio code to have -O2 optimization level, since I probably won't need to debug that, but that does not work as it seems the instantiation of everything inside does not respect the pragma.

The nature of the library makes it nearly impossible to just manually instantiate and use extern template, so I don't know what can be done about this.

2
  • For templates only functions for the types actually used with the template class will get generated. If you peek into your map file, you'll find those there, with some funky kind of name mangling. If you know the name then at least it will be possible to debug them on the assembler level. Now it might just be your debugger being s***e too, since this is the kind of functionality one expects it to have. Even if functions are inlined it should at very least show the C source for the caller function.
    – Lundin
    Commented Jun 28 at 8:25
  • You should still be able to put breakpoints on inlined/templated functions.
    – ssbssa
    Commented Jul 2 at 10:23

0

Browse other questions tagged or ask your own question.