0

I'm trying to build a project using Tensorflow Lite on my debian 11 machine, however it says I'm getting an undefined reference on some functions.

Here is the code I'm trying to run:

// Works
std::unique_ptr<tflite::FlatBufferModel> model =
        tflite::FlatBufferModel::BuildFromFile(filename);
    TFLITE_MINIMAL_CHECK(model != nullptr);

// Undefined referance:
    tflite::ops::builtin::BuiltinOpResolver resolver;
    std::unique_ptr<tflite::Interpreter> interpreter;
    tflite::InterpreterBuilder(*model, resolver)(&interpreter);

The first few lines in on itself works fine. When I add the lines below starting from BuiltinOpResolver I'm getting the following error when running make:

[ 50%] Linking CXX executable TFLiteCheck
/usr/bin/ld: CMakeFiles/TFLiteCheck.dir/main.cpp.o: in function `main':
main.cpp:(.text+0x106): undefined reference to `tflite::InterpreterBuilder::InterpreterBuilder(tflite::FlatBufferModel const&, tflite::OpResolver const&)'
/usr/bin/ld: main.cpp:(.text+0x11f): undefined reference to `tflite::InterpreterBuilder::operator()(std::unique_ptr<tflite::Interpreter, std::default_delete<tflite::Interpreter> >*)'
/usr/bin/ld: main.cpp:(.text+0x12e): undefined reference to `tflite::InterpreterBuilder::~InterpreterBuilder()'
/usr/bin/ld: main.cpp:(.text+0x19e): undefined reference to `tflite::InterpreterBuilder::~InterpreterBuilder()'
/usr/bin/ld: CMakeFiles/TFLiteCheck.dir/main.cpp.o: in function `std::default_delete<tflite::Interpreter>::operator()(tflite::Interpreter*) const':
main.cpp:(.text._ZNKSt14default_deleteIN6tflite11InterpreterEEclEPS1_[_ZNKSt14default_deleteIN6tflite11InterpreterEEclEPS1_]+0x1e): undefined reference to `tflite::Interpreter::~Interpreter()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/TFLiteCheck.dir/build.make:104: TFLiteCheck] Error 1
make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/TFLiteCheck.dir/all] Error 2
make: *** [Makefile:103: all] Error 2

I've tried [this answer][1] but it's on an arm architecture while I'm on an intel chip, and when I try it regardless I'm getting a completely different error that I've never seen before.

I've followed these steps to setup TFLite:

  1. Got the source code from tenserflow github page
  2. Got bazel version 3.7.2 (bazel-3.7.2-linux-x86_64)
  3. Ran python3 ./configure.py setting everything to default and opting to say n to everything
  4. Ran bazel build -c opt //tensorflow/lite:libtensorflowlite.so --local_ram_resources=10240 --config=noaws(Tried it with and without --local_ram_resources=10240 --config=noaws params)
  5. Move the .so file to the designated file, right next to tenserflow include files.
  6. Ran cmake .. and make on the build folder with the following CMake file:
cmake_minimum_required(VERSION 3.17)
project(TFLiteCheck)

set(CMAKE_CXX_STANDARD 14)

# include has 2 subdirectories: tensorflow and flatbuffers
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/third-party/tflite-dist/include/)

# lib has 1 file: libtensorflowlite.so
ADD_LIBRARY(tensorflowlite SHARED IMPORTED)
set_property(TARGET tensorflowlite PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/third-party/tflite-dist/libs/linux_x64/libtensorflowlite.so)

add_executable(TFLiteCheck main.cpp)
target_link_libraries(TFLiteCheck PUBLIC tensorflowlite)

And running make results in the above error. What could be the problem? Is there a better way to setup tenserflow? Like I've said only running FlatBufferModel works just fine.

Update: By removing the -J flag from the official build instructions I've managed to built the project propperly. However when I use the official cmake example:

cmake_minimum_required(VERSION 3.16)
project(minimal C CXX)

set(TENSORFLOW_SOURCE_DIR "" CACHE PATH
  "Directory that contains the TensorFlow project" )
if(NOT TENSORFLOW_SOURCE_DIR)
  get_filename_component(TENSORFLOW_SOURCE_DIR
    "${CMAKE_CURRENT_LIST_DIR}/../../../../" ABSOLUTE)
endif()

add_subdirectory(
  "${TENSORFLOW_SOURCE_DIR}/user/tensorflow_src/tensorflow/lite"
  "${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite" EXCLUDE_FROM_ALL)

add_executable(minimal main.cpp)
target_link_libraries(minimal tensorflow-lite)

When I run this with my example main.cpp using cmake . provided above, I'm getting this output and the terminal is stuck like this, without resolving:

user@debian:~/Desktop/SmartAlpha/tf_test$ cmake .
-- Setting build type to Release, for debug builds use'-DCMAKE_BUILD_TYPE=Debug'.
CMake Warning at abseil-cpp/CMakeLists.txt:70 (message):
  A future Abseil release will default ABSL_PROPAGATE_CXX_STD to ON for CMake
  3.8 and up.  We recommend enabling this option to ensure your project still
  builds correctly.


-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  

It's not frozen or anything, it just stays like this untill I hit ctrl+c to break it, without ever finishing.

Update 2: The compilation finished with this error:

user@debian:~/Desktop/SmartAlpha/tf_test$ cmake .
-- Setting build type to Release, for debug builds use'-DCMAKE_BUILD_TYPE=Debug'.
CMake Warning at abseil-cpp/CMakeLists.txt:70 (message):
  A future Abseil release will default ABSL_PROPAGATE_CXX_STD to ON for CMake
  3.8 and up.  We recommend enabling this option to ensure your project still
  builds correctly.


-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
CMake Error at eigen/CMakeLists.txt:36 (message):
  In-source builds not allowed.  Please make a new directory (called a build
  directory) and run CMake from there.  You may need to remove
  CMakeCache.txt.


-- Configuring incomplete, errors occurred!
See also "/home/user/Desktop/tf_test/CMakeFiles/CMakeOutput.log".
See also "/home/user/Desktop/tf_test/CMakeFiles/CMakeError.log".

Which cmake cache is it talking about? The one in my projects directory or the one inside my tensorflow build? Am I missing spmething? [1]: Tensorflow Lite error undefined reference to `tflite::DefaultErrorReporter()'

Turgut
  • 711
  • 3
  • 25
  • Why don't you follow an official guide on `tensorflowlite`? They have everything including an example program [right here in this link](https://www.tensorflow.org/lite/guide/build_cmake). Anyway an `undefined reference` means that you aren't linking the proper libraries. – Milan Š. Jan 25 '23 at 14:31
  • @MilanŠ. Yea that's the first thing I've tried nothing about that works, my PC just freezes every time I try to run `cmake --build . -j` as stated on that document. – Turgut Jan 25 '23 at 20:35
  • what does "my PC just freezes every time ..." mean? - could it be that it's taking up resources and looks unresponsive while it is still compiling on the background? If you run it without `-j` it won't use up all your cores (but will be slower). – Milan Š. Jan 25 '23 at 21:22
  • It actually makes sense, that it freezes and now you have undefined references, because you've probably used a limited (selective build) library as they describe in their documentation, which is missing the references that you need. – Milan Š. Jan 25 '23 at 21:25
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Milan Š. Jan 25 '23 at 21:25
  • @MilanŠ. The thing is, the way I'm building them is different, so the implementation I use where I got the undefined error is independent than the build I'm getting a freeze, thus I did not mention it on my question. It appears that the build I'm doing (with the steps provided in the question) is incomplete, hence I'm getting the error on certain functions. I might try doing it without `-J` tho I didn't knew that, slower build time is not an issue as long as it builds. – Turgut Jan 25 '23 at 21:48
  • the `-j` flag is related to your "my PC freezes" it won't magically fix your issues with undefined references. You state here: *"It appears that the build I'm doing (with the steps provided in the question) is incomplete, hence I'm getting the error on certain functions."* -> this is the answer to your question. You need to build the full library with all the objects that you need. What I recommend is following the guide I posted in the first comment and try to run it without the `-j` flag as it won't eat up your resources - it won't "freeze" – Milan Š. Jan 25 '23 at 22:11
  • @MilanŠ. Yes, but now thanks to you pointing out that removing -J I can build the complete version there is a high chance that the error will go away since I'm building the full verison. I'll keep the question updated depending on the results. – Turgut Jan 26 '23 at 08:27
  • @MilanŠ. I've tried removing -J and it actually worked. However there is another problem now, I've updated the question accordingly. As of me writing this comment, the terminal is open for half an hour now. – Turgut Jan 26 '23 at 08:55
  • @MilanŠ. I've asked a completely separate question: https://stackoverflow.com/questions/75245826/c-tensorflow-lite-undefined-reference-on-some-functions – Turgut Jan 26 '23 at 12:29

1 Answers1

0

The error suggests that you shouldn't build inside the source directory. To fix this:

Create a separate build directory outside your source tree:

mkdir build
cd build

From this build directory, call cmake pointing to your source directory:

cmake path_to_your_source_directory

After configuration with cmake, use make to build:

make

If you've already tried building in-source and you see the error related to CMakeCache.txt, you should remove this file:

rm CMakeCache.txt
Aymen Azoui
  • 369
  • 2
  • 4