Linking Problems when trying to build C application using Tensorflow Lite

Hello,
I want to make a simple application in C (not using C++) which just uses a basic pretrained model for image classification. I want to use the basic C API as described here:

For that, I have built the static library libtensorflowlite_c.a as described here: 使用 CMake 选择性构建 TensorFlow Lite
and put that into the /usr/local/lib folder.
When trying to compile this code:

#include <stdio.h>
#include "tensorflow/lite/c/c_api.h"
int main() {
    
    TfLiteInterpreterOptions* options;
    TfLiteModel* model = TfLiteModelCreateFromFile("1.tflite");
    options = TfLiteInterpreterOptionsCreate();
    TfLiteInterpreterOptionsSetNumThreads(options, 2);
}

I get errors when trying to compile using that command:

 gcc -I. -I ../../test_folder/tensorflow_src -I ../../test_folder/tensorflow_src/tensorflow/lite/c -L/usr/local/lib -l:libtensorflowlite_c.a -o ai_test ai_test.c
/usr/bin/ld: /tmp/ccQuyKaS.o: in function `main':
ai_test.c:(.text+0x17): undefined reference to `TfLiteModelCreateFromFile'
/usr/bin/ld: ai_test.c:(.text+0x25): undefined reference to `TfLiteInterpreterOptionsCreate'
/usr/bin/ld: ai_test.c:(.text+0x3a): undefined reference to `TfLiteInterpreterOptionsSetNumThreads'
collect2: error: ld returned 1 exit status

Did I misunderstand something or can somebody help me to resolve this?

Thank you in advance!

Hi Andib,

It looks like you might be missing some required dependencies when linking your application. Make sure you’re also linking against the other TensorFlow Lite libraries that libtensorflowlite_c.a depends on. Try adding -ltensorflowlite to your gcc command:

gcc -I. -I ../../test_folder/tensorflow_src -I ../../test_folder/tensorflow_src/tensorflow/lite/c -L/usr/local/lib -ltensorflowlite_c -ltensorflowlite -o ai_test ai_test.c

This should resolve the undefined references.

Hi arnoldmatt,

thank you for your reply.
Where can I get these dependencies?
When trying to execute your command, it doesn’t find that file.

gcc -I. -I ../../test_folder/tensorflow_src -I ../../test_folder/tensorflow_src/tensorflow/lite/c -L/usr/local/lib -ltensorflowlite_c -ltensorflowlite -o ai_test ai_test.c
/usr/bin/ld: cannot find -ltensorflowlite: No such file or directory
collect2: error: ld returned 1 exit status