C++ Tensorflow Lite 未定义引用

问题描述 投票:0回答:1

我正在尝试在我的 debian 11 机器上使用 Tensorflow Lite 构建一个项目,但是它说我在某些函数上得到了未定义的引用。

这是我尝试运行的代码:

// 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);

前几行本身就可以正常工作。当我从

BuiltinOpResolver
开始添加以下行时,运行
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

我已经尝试过[这个答案][1],但它是在arm架构上,而我在intel芯片上,当我尝试它时,无论如何我都会遇到一个我以前从未见过的完全不同的错误。

我已按照以下步骤设置 TFLite:

  1. 从tenserflow github页面获取源代码
  2. 获得 bazel 版本 3.7.2 (bazel-3.7.2-linux-x86_64)
  3. Ran
    python3 ./configure.py
    将所有内容设置为默认值并选择对所有内容说“n”
  4. Ran
    bazel build -c opt //tensorflow/lite:libtensorflowlite.so --local_ram_resources=10240 --config=noaws
    (尝试过使用和不使用
    --local_ram_resources=10240 --config=noaws
    参数)
  5. 将.so文件移动到指定文件,就在tenserflow包含文件旁边。
  6. 使用以下 CMake 文件在构建文件夹上运行
    cmake ..
    make
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)

运行 make 会导致上述错误。可能是什么问题呢?有没有更好的方法来设置tenserflow?就像我说过的,仅运行

FlatBufferModel
就可以了。

更新: 通过从官方构建说明中删除

-J
标志,我成功地正确构建了项目。但是当我使用官方的 cmake 示例时:

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)

当我使用上面提供的

cmake .
使用示例 main.cpp 运行此命令时,我得到此输出,并且终端像这样卡住,没有解决:

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  

它没有冻结或任何东西,它只是保持这样,直到我点击

ctrl+c
打破它,但没有完成。

更新2: 编译完成后出现此错误:

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".

它谈论的是哪个cmake缓存?我的项目目录中的那个还是我的张量流构建中的那个? 我错过了什么吗? [1]:Tensorflow Lite 错误未定义对 `tflite::DefaultErrorReporter()' 的引用

c++ tensorflow cmake bazel tensorflow-lite
1个回答
0
投票

该错误表明您不应在源目录内构建。要解决此问题:

在源代码树之外创建一个单独的构建目录:

mkdir build
cd build

从此构建目录中,调用指向您的源目录的 cmake:

cmake path_to_your_source_directory

使用cmake配置后,使用make构建:

make

如果您已经尝试构建源代码并且看到与 CMakeCache.txt 相关的错误,则应该删除此文件:

rm CMakeCache.txt
© www.soinside.com 2019 - 2024. All rights reserved.