1

I'd like to build the TensorFlow Lite as C API static library (Linux Debian x64). The instructions state the following CMake workflow:

// get the sources
git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
// create a build directory
mkdir tflite_build
cd tflite_build
// build the lib using CMake
cmake ../tensorflow_src/tensorflow/lite/c
cmake --build . -j

However, this builds the shared library libtensorflowlite_c.so.

What would be the recommended way to build static version of the C API lib? Does modifying the CMake config files require expert CMake knowledge or it could be achieved rather easy?

Danijel
  • 8,198
  • 18
  • 69
  • 133
  • 1
    Rather easy one. Add `-DTFLITE_C_BUILD_SHARED_LIBS:BOOL=OFF` to your CMake configuration step (`cmake ../tensorflow_src/tensorflow/lite/c`). Clear your build directory and rerun CMake commands. – vre Mar 18 '22 at 10:38
  • @vre Could you be more specific: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/c/CMakeLists.txt? – Danijel Mar 18 '22 at 10:58
  • Added an answer with explicit commands. – vre Mar 18 '22 at 11:02

1 Answers1

1

According to the TensorFlow Lite CMakeLists.txt file this should be rather easy. Add -DTFLITE_C_BUILD_SHARED_LIBS:BOOL=OFF to your CMake configuration step. That means, clear your build directory before doing any changes and rerun the CMake commands

cmake -S ../tensorflow_src/tensorflow/lite/c -DTFLITE_C_BUILD_SHARED_LIBS:BOOL=OFF
cmake --build . -j

Be sure to also add to your project a dependency to TF-lite and its dependencies (as mentioned here in its CMakeLists.txt) if they are not header only, as TF-lite does not provide a CMake config package (AFAIK) that otherwise would include these transitive dependencies.

vre
  • 6,041
  • 1
  • 25
  • 39
  • Works, `libtensorflowlite_c.a` is generated. What remains to be confirmed is that all symbols needed are actually linked inside the lib. Let me do a test app. – Danijel Mar 18 '22 at 11:49
  • A static library is just a bunch of objects packed together. Nothing more. The linker is not involved when creating a static library. – vre Mar 18 '22 at 11:56
  • Well, I thought so too, but look what [TensorFlow Lite C++ API document](https://www.tensorflow.org/lite/guide/build_cmake#step_5_build_tensorflow_lite) says: "Note: This generates a static library `libtensorflow-lite.a` in the current directory but the library isn't self-contained since all the transitive dependencies are not included. To use the library properly, you need to...", etc. – Danijel Mar 18 '22 at 13:22
  • In the tensorflow lite [CMakeLists.txt](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/CMakeLists.txt#L140) dependencies are resolved. You need to add those library dependencies (if they are not header only) to your minimal project too as TF-lite does not provide a CMake config package AFAIK. – vre Mar 18 '22 at 13:59