2394072_en-US

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

2394072_en-US

2394072_en-US

How to Integrate a C++ TFLite Model into a C-Based MCUXpresso Project?

Hello NXP Team,

I am integrating a TensorFlow Lite Micro model into my MCUXpresso project for the MCXN947. My application is written in C, while the TensorFlow Lite Micro inference code and generated model are in C++.

I am encountering compilation and linking issues when combining the C and C++ source files. I have already tried using "extern C", but the issue remains.

Could you please advise on the following?

Is it recommended to mix C and C++ source files in an MCUXpresso project?

What is the recommended approach for integrating a C++ TensorFlow Lite Micro model into a C-based application?

Are there any required compiler/linker settings or reference examples for this integration?

Any guidance would be greatly appreciated. Thank you.

Development BoardMCXNRe: How to Integrate a C++ TFLite Model into a C-Based MCUXpresso Project?

Hello @sivamankomb ,

Thanks for your post.

Of course you can mix C and C++ source files in an MCUXpresso project. In fact, this is also the approach used in our SDK demos. For reference, you can review several eIQ-related example projects included in the SDK. The SDK is available for download from Select Board | MCUXpresso SDK Builder.

Celeste_Liu_0-1784012914332.pngCeleste_Liu_0-1784012914332.png

The key points are as follows:

- .c files are compiled as C code.
- .cpp files are compiled as C++ code.
- The final linking stage must use a toolchain that supports the C++ runtime and C++ symbol resolution.
- C code should only call functions exposed through extern "C" wrappers and should not directly include or use TFLM C++ classes, templates, or namespaces.

Therefore, the recommended approach is to encapsulate the TFLM inference implementation in a .cpp file and expose only a C ABI-compatible wrapper interface to the C application.
For example, in the SDK's tflm_label_image demo, the core TFLM inference logic is implemented in common/tflm/model.cpp. This file uses the TFLM C++ APIs, such as

#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/micro_op_resolver.h"

static const tflite::Model* s_model = nullptr;
static tflite::MicroInterpreter* s_interpreter = nullptr;
extern tflite::MicroOpResolver &MODEL_GetOpsResolver();



Then creating a tflite::MicroInterpreter instance and calling AllocateTensors() within MODEL_Init() to perform initialization.

The externally exposed model.h,  provides a C-friendly interface.

#if defined(__cplusplus)
extern "C" {
#endif

status_t MODEL_Init(void);
uint8_t* MODEL_GetInputTensorData(tensor_dims_t* dims, tensor_type_t* type);
uint8_t* MODEL_GetOutputTensorData(tensor_dims_t* dims, tensor_type_t* type);
void MODEL_ConvertInput(uint8_t* data, tensor_dims_t* dims, tensor_type_t type);
status_t MODEL_RunInference(void);
const char* MODEL_GetModelName(void);

#if defined(__cplusplus)
}
#endif



The function declarations are exported through extern "C", allowing C source files to simply #include "model.h" and call functions such as MODEL_Init() and MODEL_RunInference() without needing any knowledge of C++ types like tflite::MicroInterpreter or MicroMutableOpResolver.
This architecture is also the approach adopted by our SDK examples and is generally recommended when integrating TFLM into a C-based application, as it cleanly isolates the C++ implementation details while preserving a pure C interface for the application layer.

If you want to integrate customer ML model to SDK demo, you can refer to AN14241 .

Hope it helps.

BR

Celeste

タグ(1)
評価なし
バージョン履歴
最終更新日:
‎07-15-2026 04:30 AM
更新者: