How to create a Tensorflow lite C Static library
Install Bazel:
TensorFlow uses Bazel as its build tool. Install Bazel by following the instructions on the official Bazel website.

Clone TensorFlow Repository:
Clone the TensorFlow repository from GitHub:


git clone https://github.com/tensorflow/tensorflow.git
Navigate to TensorFlow Lite directory:


cd tensorflow/tensorflow/lite
Build TensorFlow Lite Static Library:
Configure the build:
Configure the build with Bazel, specifying the target for a static library:


bazel build -c opt --config=monolithic tensorflow/lite:libtensorflowlite.a
This assumes that the TensorFlow Lite source code is located in the tensorflow directory.

Find the built library:
The built static library will be located in the bazel-bin/tensorflow/lite directory:


ls bazel-bin/tensorflow/lite
Look for a file named libtensorflowlite.a. @Ravitejachatti Thank you for your response here.
@Tamilarasan-C Please do follow the above comment here and refer to this guide as well
https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/tools/cmake
Thank you! Hi @Ravitejachatti and @sushreebarsa,

Thanks for the response.

@Ravitejachatti, I tried as you mentioned and I got the below error,
ERROR: Skipping 'tensorflow/lite:libtensorflowlite.a': no such package 'tensorflow/lite/tensorflow/lite': BUILD file not found in any of the following directories. Add a BUILD file to a directory to mark it as a package.
 - /home/user/new_static_build/tensorflow/tensorflow/lite/tensorflow/lite
 
Then I tried with this command "bazel build -c opt --config=monolithic //tensorflow/lite:libtensorflowlite.a" and got below error,
ERROR: Skipping '//tensorflow/lite:libtensorflowlite.a': no such target '//tensorflow/lite:libtensorflowlite.a': target 'libtensorflowlite.a' not declared in package 'tensorflow/lite' defined by /home/user/new_static_build/tensorflow/tensorflow/lite/BUILD (did you mean 'libtensorflowlite.so'? Tip: use `query "//tensorflow/lite:*"` to see all the targets in that package)

Seems like there should be details about the static library to be built in the BUILD file, please provide the required details.

Thanks. Hi @Tamilarasan-C ,

Please do check the following steps
1. Check the build file at the location '/home/user/new_static_build/tensorflow/tensorflow/lite/tensorflow/lite/BUILD'.
2. Verify that the target named **libtensorflowlite.a** is declared or not.
3. Also use bazel query **"//tensorflow/lite:*"**  as per error log to locate the target named **libtensorflowlite.a** .

Let us know the response

Thank You

 HI @LakshmiKalaKadali ,

There is no such target (libtensorflowlite.a) specified in the BUILD file.

I tried to build in a new workspace and getting a error given below:
`ERROR: An error occurred during the fetch of repository 'llvm-raw':
   Traceback (most recent call last):
        File "/home/user/bazel_static_build/tensorflow_src/third_party/repo.bzl", line 83, column 30, in _tf_http_archive_impl
                ctx.patch(patch_file, strip = 1)
Error in patch: Error applying patch /home/user/bazel_static_build/bazel_static_build/tensorflow_src/third_party/llvm/toolchains.patch: Incorrect Chunk: the chunk content doesn't match the target
**Original Position**: 89

**Original Content**:
# TODO: We should split out host vs. target here.
llvm_config_defines = os_defines + select({
    "@bazel_tools//src/conditions:windows": native_arch_defines("X86", "x86_64-pc-win32"),
    "@bazel_tools//src/conditions:darwin_arm64": native_arch_defines("AArch64", "arm64-apple-darwin"),
    "@bazel_tools//src/conditions:darwin_x86_64": native_arch_defines("X86", "x86_64-unknown-darwin"),
    "@bazel_tools//src/conditions:linux_aarch64": native_arch_defines("AArch64", "aarch64-unknown-linux-gnu"),
    "@bazel_tools//src/conditions:linux_ppc64le": native_arch_defines("PowerPC", "powerpc64le-unknown-linux-gnu"),
    "@bazel_tools//src/conditions:linux_s390x": native_arch_defines("SystemZ", "systemz-unknown-linux_gnu"),

**Revised Content**:
# TODO: We should split out host vs. target here.
llvm_config_defines = os_defines + select({
    "@bazel_tools//src/conditions:windows": native_arch_defines("X86", "x86_64-pc-win32"),
    "//llvm:macos_arm64": native_arch_defines("AArch64", "arm64-apple-darwin"),
    "//llvm:macos_x86_64": native_arch_defines("X86", "x86_64-unknown-darwin"),
    "//llvm:macos_x86_64_default": native_arch_defines("X86", "x86_64-unknown-darwin"),
    "@bazel_tools//src/conditions:linux_aarch64": native_arch_defines("AArch64", "aarch64-unknown-linux-gnu"),
    "@bazel_tools//src/conditions:linux_ppc64le": native_arch_defines("PowerPC", "powerpc64le-unknown-linux-gnu"),
    "@bazel_tools//src/conditions:linux_s390x": native_arch_defines("SystemZ", "systemz-unknown-linux_gnu"),`
    
Is there a way to modify the CMakeLists.txt file in the /tensorflow/lite directory and create a static build with cmake ?

Thank you. Hi @pkgoogle,

Please look into the issue.

 Thank You Hi @Tamilarasan-C, for this case I recommend following this https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/g3doc/guide/build_cmake.md for steps 1-3

then do 
```
cmake ../tensorflow_src/tensorflow/lite/c -DTFLITE_C_BUILD_SHARED_LIBS:BOOL=OFF
cmake --build . -j
```

this should build libtensorflowlite_c.a in your tflite_build directory:
```sh
tflite_build$ ls
abseil-cpp           compile_commands.json  fft2d              FXdiv                  ml_dtypes       pthreadpool-download  tmp
buildtests.sh        cpuinfo                flatbuffers        FXdiv-download         neon2sse        pthreadpool-source    xnnpack
check.sh             debug.sh               flatbuffers-flatc  FXdiv-source           psimd           release.sh
CMakeCache.txt       _deps                  FP16               gemmlowp               psimd-download  ruy
CMakeFiles           eigen                  FP16-download      libtensorflowlite_c.a  psimd-source    src
cmake_install.cmake  farmhash               FP16-source        Makefile               pthreadpool     tensorflow-lite
```

may I ask why you need a static library instead of a dynamic library? Hi @pkgoogle,

Thanks for the response, the reason I need a static library is because there is no file system in the deployment platform to use a shared library.

I did as you mentioned and got a static library, but while linking I still get a lot of undefined reference errors like the one below,

/usr/bin/ld: /home/user/static_build/build/libtensorflowlite_c.a(c_api.cc.o): in function `TfLiteModelCreate':
c_api.cc:(.text+0x10a): undefined reference to `tflite::DefaultErrorReporter()'

I am compiling and linking in this format,
g++ main.c -o main [library path] [library]

when I grepped for the missing reference in the library created there was a match found but I get the error too.

Thanks. Hi @Tamilarasan-C, would just creating the executable (for the correct platform) be sufficient for you? i.e. is that your true goal? (I would assume that's why you're linking). You can try using gcc instead as sometimes there's compatibility issues when linking c programs with g++ instead of gcc.

Generally it is recommended you use cmake or bazel to automatically build to an executable for you though they obviously take a little bit of start-up time to learn and use. We can try helping you achieve that if that will help you achieve your goals. Additionally, clang is now and in the future better supported.

Also if you can provide your full command (the actual library path and actual library) please do so. include any -L prefix or essentially the literal command you use, feel free to exclude any PII (like your username). Also if you are comfortable sharing your main.c file, that will help us deduce what the issue may be. Hi @pkgoogle,

Initially I am trying to do inference in linux environment (for POC) and finally it will be deployed in Xtensa (true goal).

I used gcc instead of g++ thid time but it didn't work.
This is the complete command used for building: gcc -Wall -Wextra -I/home/user/static_build_shared_lib_off/tensorflow_src -I/home/user/lib/stb -I/home/user/lib/stb/deprecated main.c -o predictor -lm -L/home/user/static_build_shared_lib_off/build -ltensorflowlite_c

Also, attaching the main.c and Makefile used to build it.
[predict.zip](https://github.com/tensorflow/tensorflow/files/13723277/predict.zip)

Attaching stb (used in the main.c file) related dependencies as well.
[stb.zip](https://github.com/tensorflow/tensorflow/files/13735438/stb.zip)

Linking this same program (main.c) with shared library works fine.

Thank you. Hi @Tamilarasan-C, can you try something like this:

```
# assuming libtensorflowlite_c.a is in the same directory
gcc -static -Wall -Wextra -I/home/user/static_build_shared_lib_off/tensorflow_src -I/home/user/lib/stb -I/home/user/lib/stb/deprecated main.c -o predictor -lm -L/home/user/static_build_shared_lib_off/build libtensorflowlite_c.a
```

Let me know how that goes or if it fails let me know the error message. Hi @pkgoogle,

Tried the command you provided and got this error,
`xxx@yyy:~/static_build_shared_lib_off/predict_c$ gcc -static -Wall -Wextra -I/home/user/static_build_shared_lib_off/tensorflow_src -I/home/user/lib/stb -I/home/user/lib/stb/deprecated main.c -o predictor -lm -L/home/user/static_build_shared_lib_off/build libtensorflowlite_c.a
gcc: error: libtensorflowlite_c.a: No such file or directory`

In the same command I used "-ltensorflowlite_c" instead of "libtensorflowlite_c.a" and got the usual undefined reference errors.

Thanks. Hi @Tamilarasan-C, was libtensorflowlite_c.a in the current directory when you tried that command? Hi @pkgoogle,

I tried the command provided by you by copying the libtensorflowlite_c.a file to the location of main.c file (current directory), but still I am getting undefined reference errors.

Also, could you please give information regarding what all packages should be added for the APIs used in main.c file if I create a custom CMakeLists.txt to build a static library.

Thank you. Hi @Tamilarasan-C, so the problem is there are actually a a lot of dependent transitive static libraries that need to be linked, https://www.tensorflow.org/lite/guide/build_cmake#step_5_build_tensorflow_lite, especially look at the 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 create a CMake project. Please refer the "Create a CMake project which uses TensorFlow Lite" section.`

I recommend getting the minimal project working and compiled on your build system: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/examples/minimal then adjusting the cmake project to your needs, the cmake project should statically link when built.

example output from the build process:
```
[  0%] Building CXX object _deps/ruy-build/ruy/CMakeFiles/ruy_denormal.dir/denormal.cc.o
[  0%] Building CXX object _deps/abseil-cpp-build/absl/time/CMakeFiles/absl_time_zone.dir/internal/cctz/src/zone_info_source.cc.o
[  0%] Building CXX object _deps/flatbuffers-build/CMakeFiles/flatbuffers.dir/src/util.cpp.o
[  0%] Building CXX object _deps/ruy-build/ruy/CMakeFiles/ruy_apply_multiplier.dir/apply_multiplier.cc.o
[  0%] Building CXX object _deps/ruy-build/ruy/CMakeFiles/ruy_have_built_path_for_avx512.dir/have_built_path_for_avx512.cc.o
[  0%] Building CXX object _deps/ruy-build/ruy/CMakeFiles/ruy_have_built_path_for_avx2_fma.dir/have_built_path_for_avx2_fma.cc.o
[  0%] Building C object _deps/cpuinfo-build/CMakeFiles/cpuinfo.dir/src/x86/isa.c.o
[  0%] Building CXX object _deps/ruy-build/ruy/CMakeFiles/ruy_have_built_path_for_avx.dir/have_built_path_for_avx.cc.o
[  0%] Building C object _deps/cpuinfo-build/CMakeFiles/cpuinfo.dir/src/x86/cache/descriptor.c.o
[  0%] Building C object _deps/cpuinfo-build/CMakeFiles/cpuinfo.dir/src/x86/cache/deterministic.c.o
[  0%] Building C object _deps/cpuinfo-build/CMakeFiles/cpuinfo.dir/src/x86/cache/init.c.o
[  0%] Building C object _deps/cpuinfo-build/CMakeFiles/cpuinfo.dir/src/x86/linux/init.c.o
[  0%] Linking CXX static library libruy_profiler_instrumentation.a
[  0%] Linking CXX static library libruy_have_built_path_for_avx.a
[  0%] Linking CXX static library libruy_have_built_path_for_avx2_fma.a
[  0%] Linking CXX static library libabsl_flags_commandlineflag_internal.a
[  0%] Building C object _deps/cpuinfo-build/CMakeFiles/cpuinfo.dir/src/x86/linux/cpuinfo.c.o
[  0%] Building C object _deps/cpuinfo-build/CMakeFiles/cpuinfo.dir/src/linux/smallfile.c.o
[  0%] Linking CXX static library libruy_have_built_path_for_avx512.a
[  0%] Building C object _deps/cpuinfo-build/CMakeFiles/cpuinfo.dir/src/linux/multiline.c.o
[  0%] Building C object _deps/cpuinfo-build/CMakeFiles/cpuinfo.dir/src/linux/processors.c.o
[  0%] Building C object _deps/cpuinfo-build/CMakeFiles/cpuinfo.dir/src/linux/cpulist.c.o
[  0%] Built target microkernel-utils
[  0%] Linking CXX static library libruy_system_aligned_alloc.a
[  0%] Linking CXX static library libruy_denormal.a
[  0%] Linking CXX static library libabsl_spinlock_wait.a
[  0%] Built target ruy_have_built_path_for_avx2_fma
[  0%] Built target ruy_profiler_instrumentation
[  0%] Built target ruy_have_built_path_for_avx
[  0%] Built target ruy_have_built_path_for_avx512
[  0%] Built target absl_flags_commandlineflag_internal
[  0%] Linking CXX static library libruy_apply_multiplier.a
[  0%] Building CXX object _deps/ruy-build/ruy/CMakeFiles/ruy_block_map.dir/block_map.cc.o
[  0%] Built target ruy_system_aligned_alloc
[  0%] Built target ruy_denormal
[  0%] Linking C static library libcpuinfo.a
[  0%] Building CXX object _deps/ruy-build/ruy/CMakeFiles/ruy_prepacked_cache.dir/prepacked_cache.cc.o
[  0%] Building CXX object _deps/ruy-build/ruy/CMakeFiles/ruy_allocator.dir/allocator.cc.o
[  0%] Built target absl_spinlock_wait
[  0%] Linking CXX static library libruy_wait.a
[  0%] Linking CXX static library libabsl_exponential_biased.a
[  0%] Linking CXX static library libabsl_strerror.a
[  0%] Linking CXX static library libabsl_log_severity.a
[  0%] Built target ruy_apply_multiplier
[  0%] Built target cpuinfo
[  0%] Built target ruy_wait
[  0%] Built target absl_exponential_biased
[  0%] Building CXX object _deps/ruy-build/ruy/CMakeFiles/ruy_cpuinfo.dir/cpuinfo.cc.o
[  0%] Building CXX object _deps/ruy-build/ruy/CMakeFiles/ruy_blocking_counter.dir/blocking_counter.cc.o
[  0%] Built target absl_strerror
[  0%] Linking CXX static library libfarmhash.a
[  0%] Built target absl_log_severity
[  0%] Linking CXX static library libruy_block_map.a
[  0%] Building CXX object _deps/abseil-cpp-build/absl/base/CMakeFiles/absl_raw_logging_internal.dir/internal/raw_logging.cc.o
[  0%] Linking CXX static library libruy_prepacked_cache.a
[  0%] Linking CXX static library libruy_allocator.a
[  0%] Built target farmhash
[  0%] Linking CXX static library libabsl_civil_time.a
[  0%] Built target ruy_block_map
[  0%] Linking CXX static library libruy_cpuinfo.a
[  0%] Built target ruy_prepacked_cache
[  0%] Built target ruy_allocator
[  0%] Linking CXX static library libruy_blocking_counter.a
[  0%] Built target absl_civil_time
[  0%] Built target ruy_cpuinfo
[  0%] Linking CXX static library libabsl_int128.a
[  7%] Building CXX object _deps/ruy-build/ruy/CMakeFiles/ruy_tune.dir/tune.cc.o
```

As you can see it statically links by default, I think this will accomplish your goals. Let me know if you have any questions. Hi @pkgoogle ,

I used the CMakeLists.txt of minimal example and added some more APIs I need to the minimal.cc file and it worked with the static library linked.

Thanks. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/62586">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/62586">No</a>
 @pkgoogle  what if I want to generate libtensorflow.a file for using the tensorflow c api . Is this the way to do this as well ?
 Hi @johntharian28, it should be -- try it out and let me know the results.
### 1. System information

- OS Platform and Distribution: Linux Ubuntu 20.04
- TensorFlow installation: Built from source
- TensorFlow library: Release 2.16.0

### 2. Query:

Hi,

I can find build instructions for Tensorflow lite C **Shared library**, I need instructions to build a **Static library** or know if there is one already built in some other location.

Thanks.