Variable initialization in C++ library header model_metadata.h

Hello there,

I’m writing to discuss an issue with the exported C++ library. I’m integrating the library in an embedded device firmware in C, and as such i followed the “inferencing c example”. While i am able to successfully use the library, there is a workaround i need to use to make it work.

The problem:

To properly feed the classifier i need to verify that i have the correct amount of features by using the defined

#define EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE       (EI_CLASSIFIER_RAW_SAMPLE_COUNT * EI_CLASSIFIER_RAW_SAMPLES_PER_FRAME)

this definition is in the model_metadata.h file where there also are 2 variable initializations, for example,

const char* ei_classifier_inferencing_categories[] = { "idle", "snake", "updown", "wave" };

So, when we include this header we risk the multiple definition of … when trying to link it.

It works fine as a shared library because in this case there will be 2 copies of the same data (1 in the library and other in the application).

The workaround:
I compile the library using ar do create an archive of objects, then modify the model_metadata header removing the initialization from the header, and modifying the declarations to external, and that way i can use it in the application.

My suggestion:
Remove this initialization from the header file (although this may require more extensive and complex modifications to the library).
or
Move the macro definitions from this header file to a new one, using an include in model_metadata (so that one can use the the macros without having to include the initialized variables). EDIT: actually this is not a solution because it is included through ei_classifier_types.h which has the result types

I’m trying to use the library as a black box, so that future updates to the classifier can be used without all these steps.

I may be wrong at some point but afaik initializing variables in header files is not recommended practice exactly because of this issue.

Thanks for reading

Hi @TiagoNascJBay yeah great suggestion, and one that we’re tracking for a while already. It’s not just the categories, but also the DSP block configs here. Unfortunately it’s not super easy to fix this in a way that does not affect all our current users, but we have a tracking bug for the embedded team.

Hi, thanks for the reply.
It is great news for me that it is already being tracked. It would remove quite a few steps from my current workaround. Is there anywhere i can “subscribe” for updates on this issue ?

I’ve added a link to this post to the ticket - will make sure to ping you here if we have solved it.

1 Like

Hi @TiagoNascJBay 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).

Hi @janjongboom, thats great news. This will probably break some of my workaround scripts tho. :sweat_smile: Thanks for the heads up.

Hi, just used the new structure and it appears to resolve the initial issue that i reported.