Some SDK header files can't be included f̶r̶o̶m̶ ̶C̶+̶+̶

In some cases, even basic header files can’t be included from cpp files. For example, attempting to include “model_metadata.h” will result in linker errors for multiple definitions. Trying to extern “C” will result in “Template with c linkage” errors as these header files seem to attempt to deal with C++, and in doing so pull in C++ constructs.

EDIT: also model_metadata doesn’t include stdbool.h so you can’t simply include it without first including stdbool.h.

EDIT2: the difficulty is in part because there are definitions and not just declarations in the SDK header files, making it impossible to include them from more than one compilation unit. I suspect this is why the ei_microphone is required to perform mallocs during init. Whomever wrote this probably ran into the same thing and wasn’t able to directly reference the slice lengths from the metadata.

@jefffhaynes Thanks for the feedback! Would you be able to provide more context as to which toolchain you are experiencing this with?

Sure. arm-none-eabi-gcc.exe (GNU Arm Embedded Toolchain 10-2020-q4-major) 10.2.1 20201103 (release)

However, if you see “EDIT2”, I think the central problem will affect all compilers.

Thanks

Yes, thanks! As I understand it ARM gcc is used as the underlying compiler for a lot of different build environments. It would be great to get more insight into how you’ve setup your project as the best starting point are our standalone inferencing projects for the various platforms we support. Can you share which target this is for?

I’m using nrf52840 and nrf5380

Gotcha, then I assume you have looked at: https://docs.edgeimpulse.com/docs/running-your-impulse-locally-zephyr. Just wondering how your setup differs from this.

Hi @jefffhaynes we’re aware of the issue with model_metadata.h and the defined variables there, however it’s really hard to fix in a way that doesn’t break backwards compatibility with all existing projects / Makefiles / etc. Over time we’ll be switching to a model structure that holds everything (and lets you define multiple models to run on the same MCU), but that’s not here yet.

My suggestion would be to just include ei_run_classifier.h and then let it include everything else.

Thanks @janjongboom. I’m not in front of my computer right now but I believe ei_run_classifier suffers from the same problem, at least with respect to c++. I’ll try it a bit later and confirm. Running multiple models would be awesome!

I see what you’re saying. After some refactoring I was able to get everything down to a single include and was able to get rid of that malloc. Thanks.

2 Likes

Hi @jefffhaynes we’ve changed the model metadata format (will be live later this week) which allows you to include it multiple times. The variable declarations are moved into another header file (model_variables.h, still a similar issue, but easier to get around for applications).

1 Like