I've successfully built a simple C++ app running TF Lite model by adding my sources to tensorflow/lite/examples
, similarly to what the official C++ TF guide suggests for full TF. Now I want to build it as a separate project (shared library) linking to TF Lite statically and using CMake as a build system.
I tried to add a custom target to my CMakeLists.txt
, which would build TF Lite with Bazel:
set(TENSORFLOW_DIR ${CMAKE_SOURCE_DIR}/thirdparty/tensorflow)
add_custom_target(TFLite
COMMAND bazel build //tensorflow/lite:framework
COMMAND bazel build //tensorflow/lite/kernels:builtin_ops
WORKING_DIRECTORY ${TENSORFLOW_DIR})
I chose those Bazel targets because the BUILD
file from tensorflow/lite/examples/minimal
has them as dependencies, and they work for me when I build
my code with Bazel within TF repo. Not sure if that's enough.
Then I manually collect include dirs (with that ugly temporarily hardcoded path) and libs:
set(TFLite_INCLUDES
${TENSORFLOW_DIR}
~/.cache/bazel/_bazel_azymohliad/ec8567b83922796adb8477fcbb00a36a/external/flatbuffers/include)
set(TFLite_LIBS
${TENSORFLOW_DIR}/bazel-bin/tensorflow/lite/libframework.pic.a)
target_include_directories(MyLib ... PRIVATE ... ${TFLite_INCLUDES})
target_link_libraries(MyLib ... ${TFLite_LIBS})
And with this configuration, I get many undefined references to TFLite stuff during linkage. I checked with nm
and those symbols are indeed missing in libframework.pic.a
, I found some of them in various .o
files in Bazel output. Manually picking all those .o
files seems wrong.
So, is it possible to link nicely to TF Lite from CMake like I'm trying to? Maybe is there some magical bazel query include_dirs(//tensorflow/lite:framework)
command which would give me paths to all the necessary include dirs, and a similar command for libraries to link against so that I could pass this info to CMake?